这是一个 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-on-rails - 如何在 Ruby on Rails 中实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3393790/