Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>

</dependencies>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/emarsys/escher/Escher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.emarsys.escher.util.DateTime;
import org.apache.http.client.utils.URIBuilder;

import javax.xml.bind.DatatypeConverter;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -14,6 +13,8 @@
import java.util.Map;
import java.util.function.Consumer;

import static org.apache.commons.codec.binary.Hex.encodeHexString;

public class Escher {

public static final String UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD";
Expand Down Expand Up @@ -122,7 +123,7 @@ private String calculateSignature(Helper helper, EscherRequest request, String s

Logger.log("Canonicalized request: " + canonicalizedRequest);
Logger.log("String to sign: " + stringToSign);
Logger.log("Signing key: " + DatatypeConverter.printHexBinary(signingKey));
Logger.log("Signing key: " + encodeHexString(signingKey, true));
Logger.log("Signature: " + signature);

return signature;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/emarsys/escher/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URLEncodedUtils;

import javax.xml.bind.DatatypeConverter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
Expand All @@ -16,6 +15,8 @@
import java.util.function.BinaryOperator;
import java.util.function.Predicate;

import static org.apache.commons.codec.binary.Hex.encodeHexString;


class Helper {

Expand Down Expand Up @@ -132,7 +133,7 @@ public String calculateAuthHeader(String accessKeyId, Instant date, String crede


public String calculateSignature(byte[] signingKey, String stringToSign) throws EscherException {
return DatatypeConverter.printHexBinary(Hmac.sign(config.getHashAlgo(), signingKey, stringToSign)).toLowerCase();
return encodeHexString(Hmac.sign(config.getHashAlgo(), signingKey, stringToSign), true);
}


Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/emarsys/escher/util/Hmac.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package com.emarsys.escher.util;

import com.emarsys.escher.EscherException;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.nio.charset.Charset;
import java.security.MessageDigest;

public class Hmac {

private static final Charset UTF8 = Charset.forName("UTF-8");

private static final String HASH_ALGO = "SHA-256";

public static String hash(String text) throws EscherException {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(text.getBytes(UTF8));
byte[] bytes = md.digest();
return DatatypeConverter.printHexBinary(bytes).toLowerCase();
MessageDigest md = MessageDigest.getInstance(HASH_ALGO);
return new DigestUtils(md).digestAsHex(text).toLowerCase();
} catch (Exception e) {
throw new EscherException("Unable to compute hash", e);
throw new EscherException("Unable to compute hash: " + e.getMessage(), e);
}
}


public static byte[] sign(String hashAlgo, String key, String data) throws EscherException {
return sign(hashAlgo, key.getBytes(UTF8), data);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/emarsys/escher/HelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.xml.bind.DatatypeConverter;
import java.net.URI;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.commons.codec.binary.Hex.encodeHexString;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
Expand All @@ -35,7 +35,7 @@ public void testCalculateSigningKey() throws Exception {
);

assertThat(
DatatypeConverter.printHexBinary(signingKey).toLowerCase(),
encodeHexString(signingKey, true),
is("98f1d889fec4f4421adc522bab0ce1f82e6929c262ed15e5a94c90efd1e3b0e7")
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/emarsys/escher/util/HmacTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.emarsys.escher.util;

import com.emarsys.escher.EscherException;
import org.junit.Assert;
import org.junit.Test;


public class HmacTest {

@Test
public void testHashGenerationSHA256() throws EscherException {
String textToHash = "test text";
String expectedHash = "0f46738ebed370c5c52ee0ad96dec8f459fb901c2ca4e285211eddf903bf1598";

Assert.assertEquals(expectedHash, Hmac.hash(textToHash));
}

}