Using HMAC SHA256 in Ruby

20,906

Solution 1

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"

Solution 2

Try This:

hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), key, data)
Share:
20,906

Related videos on Youtube

Eduardo Pedroso
Author by

Eduardo Pedroso

I'm a passionate Engineer. Writing code is my favorite activity and I have a lot of fun while doing it :) In my career, I've worked from small startups to Fortune 500 companies. I built from simple web pages to complex high scalable apps and multi-tab browsers. My preferred stack is around JavaScript (both client and server), but I also love Python and Functional Languages. I love working on fast-paced environments and I'm always looking for an opportunity to work with smart and passionate people that are whiling to share knowledge and deliver an amazing piece of software!

Updated on July 27, 2022

Comments

  • Eduardo Pedroso
    Eduardo Pedroso almost 2 years

    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?

    • Mike Szyndel
      Mike Szyndel over 8 years
      According to the documentation digest: Returns the authentication code an instance represents as a binary string.
    • Mike Szyndel
      Mike Szyndel over 8 years
      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)
    • Eduardo Pedroso
      Eduardo Pedroso over 8 years
      Worked just fine with hexdigest! Thank You
    • Mike Szyndel
      Mike Szyndel over 8 years
      Since I fixed your problem it would be nice if you let me answer instead of doing it yourself.
    • Eduardo Pedroso
      Eduardo Pedroso over 8 years
      Sorry @MichalSzyndel, already delete the answer
  • cmunozgar
    cmunozgar about 5 years
    In this case to make it HMAC SHA256 you need to put digest = OpenSSL::Digest.new('sha256') in the lower comment: stackoverflow.com/a/42832500/4706812
  • Mike Szyndel
    Mike Szyndel almost 5 years
    Updated the answer @cmunozgar, not sure why I put sha1 in there in the first place
  • Dat Le Tien
    Dat Le Tien about 2 years
    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/…