如何在Ruby on rails上做到这一点
这是一个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上做同样的事情? 更具体地说,我需要知道从字符串获取字节的函数,base64对它们进行编码并计算hmac散列。
不确定它是否完全相同 ,但它适用于我:
@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))
我会再尝试。
有一些用于ruby / rails的HMAC库可能会使这更简单: http : //auth-hmac.rubyforge.org/