这是一个C#代码:
byte[] pb = System.Text.Encoding.UTF8.GetBytes(policy.ToString());
// Encode those UTF-8 bytes using Base64
string policyB = Convert.ToBase64String(pb);
// Sign the policy with your Secret Key using HMAC SHA-1.
System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1();
hmac.Key = System.Text.Encoding.UTF8.GetBytes(secretKey);
byte[] signb = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(policyB));
string signature = Convert.ToBase64String(signb);
如何在Ruby on rails上做同样的事情?更具体地说,我需要知道从string获取字节的函数,base64对它们进行编码并计算hmac hash。
答案 0 :(得分:4)
不确定完全是否相同,但它对我有用:
@policy = ActiveSupport::Base64.encode64s(@policy)
# Sign policy with secret key
digest = OpenSSL::Digest::Digest.new('sha1')
@signature = ActiveSupport::Base64.encode64s(OpenSSL::HMAC.digest(digest, secretKey, @policy))
答案 1 :(得分:2)
我会再试一次。
有一些用于ruby / rails的HMAC库可能会使这更简单: http://auth-hmac.rubyforge.org/