Using HMAC SHA256 in Ruby

前端 未结 3 1788
温柔的废话
温柔的废话 2020-12-29 05:57

I\'m trying to apply HMAC-SHA256 for generate a key for an Rest API.

I\'m doing something like this:

def generateTransactionHash(stringToHash)
  key          


        
3条回答
  •  执念已碎
    2020-12-29 06:33

    In my case (Ticketmatic) I had to create the HMAC like above and add an Authorization header to the request with the HMAC in it.

    hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), secret_key, access_key + name + time)
    req = Net::HTTP::Get.new(uri)
    req['Authorization'] = "TM-HMAC-SHA256 key=#{access_key} ts=#{time} sign=#{hmac}"
    res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
    

    You can find a full gist here

    And a blogpost with more explantion here

提交回复
热议问题