Using HMAC SHA256 in Ruby
Asked Answered
B

4

31

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 = '123'
  data = 'stringToHash'
  digest = OpenSSL::Digest.new('sha256')

  hmac = OpenSSL::HMAC.digest(digest, key, data)
  puts hmac
end

The output of this is always this: (if I put '12345' as parameter or 'HUSYED815X', I do get the same)

ۯw/{o���p�T����:��a�h��E|q

The API is not working because of this... Can some one help me with that?

Berkeleianism answered 18/1, 2016 at 12:41 Comment(5)
According to the documentation digest: Returns the authentication code an instance represents as a binary string.Indescribable
Maybe you should use hexdigest instead, it has the same signature as digest but returns hex-encoded string (from the docs it looks like it's the same string but human readable)Indescribable
Worked just fine with hexdigest! Thank YouBerkeleianism
Since I fixed your problem it would be nice if you let me answer instead of doing it yourself.Indescribable
Sorry @MichalSzyndel, already delete the answerBerkeleianism
I
57

According to the documentation OpenSSL::HMAC.digest

Returns the authentication code an instance represents as a binary string.

If you have a problem using that maybe you need a hex encoded form provided by OpenSSL::HMAC.hexdigest

Example

key = 'key'
data = 'The quick brown fox jumps over the lazy dog'
digest = OpenSSL::Digest.new('sha256')

OpenSSL::HMAC.digest(digest, key, data)
#=> "\xF7\xBC\x83\xF40S\x84$\xB12\x98\xE6\xAAo\xB1C\xEFMY\xA1IF\x17Y\x97G\x9D\xBC-\x1A<\xD8"

OpenSSL::HMAC.hexdigest(digest, key, data)
#=> "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
Indescribable answered 19/1, 2016 at 12:47 Comment(3)
In this case to make it HMAC SHA256 you need to put digest = OpenSSL::Digest.new('sha256') in the lower comment: https://mcmap.net/q/460363/-using-hmac-sha256-in-rubyCladding
Updated the answer @cmunozgar, not sure why I put sha1 in there in the first placeIndescribable
No need to create a digest instance, just put a string represents the algorithm and it works like a charm OpenSSL::HMAC.hexdigest('sha256', key, data) since ruby 2.5 ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/…Shalon
O
20

Try This:

hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, data)
Oceanography answered 16/3, 2017 at 11:14 Comment(0)
U
0
   def make_payment(user)
    @key= SecureRandom.hex(10)
    #puts @key
    @secret_key = @key
    puts " this is the  public key #{@secret_key}"
    @access_key= generate_key
    puts " this is the access key #{@access_key}"
    @name= @user.name
    puts "#{@name}"
    @time= Time.now.in_time_zone("Nairobi")
    puts "This is the time request sent #{@time}"
    @server_key = SecureRandom.base64
    puts "This is the server key #{@server_key}"
    @data = 'This request is being made from Learnida for users to make a payment'
    @digest = OpenSSL::Digest.new('sha256')
    uri = URI.parse("https://learnida.com")

    @hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret_key, @access_key)
     puts "This is the HMAC #{@hmac}"
    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) }
    @hmacdigest= OpenSSL::HMAC.digest(@digest, @server_key, @data)
    puts" This is the HMAC:SHA-256:   #{@hmacdigest}" 
    #puts res.body
    #=> "\xF7\xBC\x83\xF40S\x84$\xB12\x98\xE6\xAAo\xB1C\xEFMY\xA1IF\x17Y\x97G\x9D\xBC-\x1A<\xD8"
    @sslkey= OpenSSL::HMAC.hexdigest(@digest, @server_key, @data)
    puts @sslkey
Uncivil answered 2/1, 2023 at 8:11 Comment(1)
This is how you can use Open SSL and HMAC in the headers with assigned keyUncivil
A
-1

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

Asuncion answered 26/10, 2018 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.