I am trying to reproduce HMAC signature described here.
The example is using crypt from nodejs, however, in my case, I need to use the google closure library. So I tried to reproduce the HMAC signature using the goog.crypt.Hmac library.
Below are my experiment code.
let crypto = require('crypto');
require("google-closure-library");
goog.require("goog.crypt.Hmac");
goog.require("goog.crypt");
goog.require('goog.crypt.Sha1');
function forceUnicodeEncoding(string) {
return decodeURIComponent(encodeURIComponent(string));
}
function nodejs_crypto(string_to_sign, secret) {
signature = crypto.createHmac('sha1', secret)
.update(forceUnicodeEncoding(string_to_sign))
.digest('base64')
.trim();
return signature
}
function goog_crypto(string_to_sign, secret) {
const hmac = new goog.crypt.Hmac(new goog.crypt.Sha1(), goog.crypt.stringToByteArray(secret));
const hash = hmac.getHmac(forceUnicodeEncoding(string_to_sign));
return hash.toString()
}
const string_to_sign = "message";
const secret = "secret";
const sig1 = nodejs_crypto(string_to_sign, secret);
const sig2 = goog_crypto(string_to_sign, secret);
console.log(sig1);
// DK9kn+7klT2Hv5A6wRdsReAo3xY=
console.log(sig2);
// 12,175,100,159,238,228,149,61,135,191,144,58,193,23,108,69,224,40,223,22
I can hardly find any examples online for goog.crypt.Hmac.
Here are my problems:
- I am not sure if
goog_cryptois implemented correctly. - why
hash.String()returns an array-like thing? - how can I convert the hmac hash to base64 string using the closure lib.
getHmac, i.ehashin your code, is an array of integers - as documented here ... anarray.toString()is much the same asarray.join()i.e
Since
hashis an array ofNumbers,Buffer.from(hash)creates a Buffer from the numbers inhash-buffer.toString('base64')returns the buffer data encoded in base64Regarding point 1: to prove the results you are getting in your code are the same