I am trying to recreate a Java algorithm in Mathematica. It is taking a key and signing a message using the HMAC and SHA256 algorithm.
The Java code for which I am basing it off is here
I have dragged the apache commons 1.11 jar to (using a mac) /Applications/Mathematica.app/Contents/SystemFiles/Converters/Java/
Needs["JLink`"]
InstallJava[];
Mac = LoadJavaClass["javax.crypto.Mac"];
secretKeySpec = LoadJavaClass["javax.crypto.spec.SecretKeySpec"];
base64 = LoadJavaClass["org.apache.commons.codec.binary.Base64"];
key = "DDDD20942134FBB95AAF88198F260E06";
message =
"gethttps://api.gtcprojects.com/trade/orders1507882361.6882524";
keyJava = JavaNew["java.lang.String", key]
messageJava = JavaNew["java.lang.String", message]
Which gives
« JavaObject[java.lang.String]»
« JavaObject[java.lang.String]»
keyBytes = keyJava@getBytes[]
messageBytes = messageJava@getBytes[]
Which gives
{68, 68, 68, 68, 50, 48, 57, 52, 50, 49, 51, 52, 70, 66, 66, 57, 53, 65, 65, 70, 56, 56, 49, 57, 56, 70, 50, 54, 48, 69, 48, 54}
{103, 101, 116, 104, 116, 116, 112, 115, 58, 47, 47, 97, 112, 105, 46, 103, 116, 99, 112, 114, 111, 106, 101, 99, 116, 115, 46, 99, 111, 109, 47, 116, 114, 97, 100, 101, 47, 111, 114, 100, 101, 114, 115, 49, 53, 48, 55, 56, 56, 50, 51, 54, 49, 46, 54, 56, 56, 50, 53, 50, 52}
signingKey = JavaNew[secretKeySpec, keyBytes, "HmacSHA256"]
Which gives
« JavaObject[javax.crypto.spec.SecretKeySpec]»
mac = Mac`getInstance["HmacSHA256"]
Which gives
« JavaObject[javax.crypto.Mac]»
mac@init[signingKey]
test = mac@doFinal[messageBytes]
Which gives
{47, 105, 21, 95, -108, -119, 124, -33, -39, -98, 79, -98, 4, 87, 44, 88, 67, -53, 96, -1, 120, 91, 89, 114, -72, 23, -11, -115, 109, -24, -25, -39}
It appears everything is going well until I need to base64 encode it.
base64`encodeBase64String[test]
Which gives:
base64`encodeBase64String[{47, 105, 21, 95, -108, -119, 124, -33, -39, -98, 79, -98, 4, 87, 44, 88, 67, -53, 96, -1, 120, 91, 89, 114, -72, 23, -11, -115, 109, -24, -25, -39}]
It is also a little scary that there is negative bytes (CharacterCodes in Mathematica) in the results of the HMAC.
The correct answer is (confirmed by python)
L2kVX5SJfN/Znk+eBFcsWEPLYP94W1lyuBf1jW3o59k=
Base64`encodeBase64String[...]
(starting with a capital B)? $\endgroup$Developer`EncodeBase64[test]
$\endgroup$