7

Using Google + Bing didn't yield an answer to what should be a simple question:

How are you supposed to use the HMAC module in Ruby to create a HMAC with MD5 (that uses a secret)?

The HMAC docs seem awfully thin.

Thanks!

Crashalot
  • 33,605
  • 61
  • 269
  • 439

6 Answers6

14

This should be the easiest way:

OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('md5'), secret_key, your_data)
mirza
  • 952
  • 1
  • 9
  • 11
7

The following gem should be installed: 'ruby-hmac'

$ irb
>> require 'hmac-md5'
=> true
>> HMAC::MD5.new("abc").digest
=> "\324\035\214\331\217\000\262\004\351\200\t\230\354\370B~"
>> HMAC::MD5.new("abc").hexdigest
=> "d41d8cd98f00b204e9800998ecf8427e"
>> 
vvladymyrov
  • 5,715
  • 2
  • 32
  • 50
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
4

In 2020 this is how I did it:

OpenSSL::HMAC.hexdigest("MD5", secret_key, your_data)

Ruby documentation is available here: OpenSSL::HMAC

Dagmar
  • 2,968
  • 23
  • 27
4

This is what I did:

HMAC::MD5.new(shared_key).update(data).hexdigest
paulnsorensen
  • 448
  • 4
  • 10
1

Probably you just want HMAC::MD5.new(SECRET).digest

Lookup "salting" a hash first. It depends on your usage, but adding a fixed string does help by making your hashes different than the hashes from other apps. Thus, a dictionary attack is harder. But that's just generally speaking.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
JEremy
  • 11
  • 1
-1

http://betterlogic.com/roger/?p=152

no?

Or I remember toying with it inside one of our Gems so maybe you can reverse engineer from it?

http://github.com/appoxy/aws/tree/master

Hope this helps.

Chad

Chad
  • 1,818
  • 2
  • 15
  • 23