0
public static String calculateRFC2104HMAC(String data, String key)
throws java.security.SignatureException, java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException {
String result;
// get an hmac_sha1 key from the raw key bytes javax.crypto.spec.SecretKeySpec

signingKey = new javax.crypto.spec.SecretKeySpec(key.getBytes(), \"HmacSHA256\");

// get an hmac_sha1 Mac instance and initialize with the signing key javax.crypto.Mac

mac = javax.crypto.Mac.getInstance(\"HmacSHA256\"); mac .init(signingKey);

// compute the hmac on input data bytes

byte[] rawHmac = mac .doFinal(data.getBytes());
// base64-encode the hmac

result = org.apache.commons.codec.binary.Base64.encodeBase64String(rawHmac);

result = java.net.URLEncoder.encode(result.trim(),\"UTF-8\");

return result;
}

I am having a difficult time implementing this algorithm into Javascript. I have a example to work with but I am doing something wrong along the way and it never outputs the correct hash_code.

here is the example test case I have to work with.

data: V2/assets?ipGLN=1234&senderGLN=1234&app_id=3cea2db2&TIMESTAMP=2012-12- 06T13:08:02Z

key: 6b904a0c5ceefa991b2ebc9cfec202b6

Generated Hash Code i.e. result: 4F7rjpsRpR0IEy12EfAFShjS9VOxpisVgb8ywavqRrI%3D

So request URL with hash_code parameter will be:

https://digasset.preprod.1worldsync.com/V2/assets?ipGLN=1234&senderGLN=1234&app_id=3cea2db2&TIMESTAMP=2012-12-06T13:08:02Z&hash_code=4F7rjpsRpR0IEy12EfAFShjS9VOxpisVgb8ywavqRrI%3D

If anyone has any insight into implementing the above correctly, I would greatly appreciate it.

Here is my implementation so far..

var temp_test_key = "";
var test_key_bytes = encodeTextToUtf8(test_key);
for(var i = 0;i<test_key_bytes.length;i++)
{
    temp_test_key+= test_key_bytes[i];
}  
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256,temp_test_key);
hmac.update(test_string);
var hash = hmac.finalize();
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
document.write(hashInBase64);

Thank you.

I have even tried to redo this into vb.net.. but it is still not yeilding the same result..

Function SignMessage(key As String, message As String) As Byte()
    Dim encText As New System.Text.UTF8Encoding
    Dim btKey() As Byte, btMessage() As Byte
    btKey = encText.GetBytes(key)
    Dim myhmac As New HMACSHA256(btKey)
    btMessage = encText.GetBytes(message)
    Return myhmac.ComputeHash(btMessage)
End Function


Sub Main()
    Dim testString As String = "V2/assets?ipGLN=1234&senderGLN=1234&app_id=3cea2db2&TIMESTAMP=2012-12-06T13:08:02Z"
    Dim Hash() As Byte = SignMessage("6b904a0c5ceefa991b2ebc9cfec202b", testString)
    MsgBox(System.Convert.ToBase64String(Hash, 0, Hash.Length))

End Sub
BanMe
  • 133
  • 1
  • 8
  • which crypto library are you using? Is it [crypto-js](https://code.google.com/p/crypto-js/) or is it node [crypto](http://nodejs.org/api/crypto.html#crypto_class_cipher)? – Tri Chu Feb 10 '15 at 21:53
  • CryptoJS is the library. – BanMe Feb 10 '15 at 22:18
  • Your generated hash key example seem to be incorrect. When I ran the data and key on this online [tool](http://www.freeformatter.com/hmac-generator.html#ad-output) and [this](http://tomeko.net/online_tools/hex_to_base64.php?lang=en) it generated a totally different string – Tri Chu Feb 10 '15 at 22:44
  • I tried both of those tools but even then that didnt get the result the example shows.. – BanMe Feb 11 '15 at 06:26
  • I made a jsfiddle to run the same hash and the result is not what the example show [link](http://jsfiddle.net/Lu4ryapg/). My guess is the input "data" is not actually the whole URI string but just bit of data in some order. – Tri Chu Feb 11 '15 at 06:45

0 Answers0