How to do it in Ruby on rails

This is the C # code:

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); 

How to do the same in Ruby on rails? In particular, I need to know the functions in order to get the bytes from the string and base64, encode them and calculate the hash hash.

+1
source share
2 answers

Not sure if it is exactly the same, but it works for me:

 @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)) 
+4
source

I'll try again.

There are several HMAC libraries for rubies / rails that can make this much easier: http://auth-hmac.rubyforge.org/

+2
source

Source: https://habr.com/ru/post/905468/


All Articles