我正在使用 ApiAuth gem(如在此处找到)来签署我的请求。我还在使用 CryptoJS(如在此处找到)编写自己的 JavaScript 代码,通过检查 ApiAuth 生成的加密标头与我的代码生成的标头来提供身份验证。
下面给出了来自 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
这是我用 JavaScript 编写的等效代码:
function hmacSignature(request, appSecret) {
return CryptoJS.HmacSHA1(canonicalString(request), appSecret).toString(CryptoJS.enc.Base64);}
这两个不会生成相同的加密标头。我尝试使用 jsSHA 来做同样的事情,虽然 jsSHA 和 CryptoJS 生成的加密标头是相同的,但它们与 ApiAuth 生成的标头不匹配。
请帮我弄清楚如何使这项工作。
编辑:
将 Canonical String 作为“消息”并将 appSecret 作为“secret”,我从 ApiAuth 和 CryptoJS 获得相同的值,即:
DK9kn+7klT2Hv5A6wRdsReAo3xY=
我发现我的原始代码中的问题即将出现,因为我的 JS 代码中设置的时间戳和 ApiAuth 中设置的时间戳不匹配。