How to create MD5 hash with HMAC module in Ruby?

我怕爱的太早我们不能终老 提交于 2019-11-29 03:42:06

问题


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!


回答1:


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"
>> 



回答2:


This should be the easiest way:

OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('md5'), secret_key, your_data)



回答3:


This is what I did:

HMAC::MD5.new(shared_key).update(data).hexdigest



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/1359624/how-to-create-md5-hash-with-hmac-module-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!