I am using ApiAuth gem (as found here) to sign my request. I am also writing my own JavaScript code using CryptoJS (as found here) to provide authentication by checking the encrypted header generated by ApiAuth against the one generated by my code.
Given below is a code snippet from ApiAuth Gem:
def hmac_signature(headers, secret_key, options)
if options[:with_http_method]
canonical_string = headers.canonical_string_with_http_method(options[:override_http_method])
else
canonical_string = headers.canonical_string
end
digest = OpenSSL::Digest.new('sha1')
b64_encode(OpenSSL::HMAC.digest(digest, secret_key, canonical_string))
end
Here is the code I have written as an equivalent in JavaScript:
function hmacSignature(request, appSecret) {
return CryptoJS.HmacSHA1(canonicalString(request), appSecret).toString(CryptoJS.enc.Base64);}
These two don't generate the same encrypted header. I tried using jsSHA to do the same thing and while the encrypted header generated by jsSHA and CryptoJS is the same, they don't match the one generated by ApiAuth.
Kindly help me figure out how to make this work.
EDIT:
Taking Canonical String as "message" and appSecret as "secret" I get the same values from ApiAuth and CryptoJS which is:
DK9kn+7klT2Hv5A6wRdsReAo3xY=
I've figured out that the problem in my original code is coming because the timestamp set in my JS code and the one set in the ApiAuth don't match.