Using HMAC SHA256 in Ruby
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)
Related videos on Youtube
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, 2022Comments
-
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 over 8 yearsAccording to the documentation
digest: Returns the authentication code an instance represents as a binary string.
-
Mike Szyndel over 8 yearsMaybe you should use
hexdigest
instead, it has the same signature asdigest
but returns hex-encoded string (from the docs it looks like it's the same string but human readable) -
Eduardo Pedroso over 8 yearsWorked just fine with hexdigest! Thank You
-
Mike Szyndel over 8 yearsSince I fixed your problem it would be nice if you let me answer instead of doing it yourself.
-
Eduardo Pedroso over 8 yearsSorry @MichalSzyndel, already delete the answer
-
-
cmunozgar about 5 yearsIn 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 almost 5 yearsUpdated the answer @cmunozgar, not sure why I put sha1 in there in the first place
-
Dat Le Tien about 2 yearsNo 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/…