That is how i am generating hmacsha1 signature in java
private static byte[] hmac_sha1(String crypto, byte[] keyBytes, byte[] text) {
Mac hmac = null;
try {
hmac = Mac.getInstance(crypto);
SecretKeySpec macKey =
new SecretKeySpec(keyBytes, "RAW");
hmac.init(macKey);
System.out.println("hmac: "+Arrays.toString(keyBytes));
return hmac.doFinal(text);
} catch (Exception e) {
// NOTE. Deviation from reference code.
// Reference code prints a stack trace here, which is not what we
// want in a production environment, so instead we rethrow.
throw new UndeclaredThrowableException(e);
}
}
I need help to generate same in node.js. Can someone please help me on this? As people mentioned that I need to show what I have tried here is the code that I have written in node.js to create a same functionality
Ocra.hmacSha1 = function(crypto, keyBytes, text) {
var digest, hmac;
hmac = nodeCrypto.createHmac(crypto, new Buffer(keyBytes, 'utf8'));
console.log(this.bin2String(keyBytes));
digest = hmac.update(new Buffer(text, 'utf8')).digest('hex');
return this.hexStr2Bytes(digest); // here i am converting string into bytes array
};
The above code is not producing the desired results. If I pass these parameters to java code crypto: sha1 keyBytes: [ 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48 ] text:79678265454958727984804583726549455458817848560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
they produce different result and in node.js it produces different results.
Note: In java crypto is HmacSHA1 and text is in form of array not string as you can see in code as well.
What are you passing as keyBytes in the JavaScript version? new Buffer(keyBytes, 'utf8') is almost certainly not what you want. If you're passing a hex encoded string, you need to hex decode it: new Buffer(keyBytes, 'hex'). If you're passing an array, you have to do new Buffer(keyBytes).