Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create MD5 hash with HMAC module in Ruby?

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!

like image 221
Crashalot Avatar asked Aug 31 '09 22:08

Crashalot


2 Answers

This should be the easiest way:

OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('md5'), secret_key, your_data)
like image 160
mirza Avatar answered Nov 02 '22 19:11

mirza


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"
>> 
like image 30
DigitalRoss Avatar answered Nov 02 '22 20:11

DigitalRoss