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:
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