Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

digest/hmac is part of ruby standard lib

Tags:

ruby

gem

hmac

i'm working with some codes that has a:

begin
require 'digest/hmac'
USE_EMBEDDED_HMAC = false
rescue
puts "HMAC, not found in standard lib." + $!.message
require 'hmac-sha1'
USE_EMBEDDED_HMAC = true
end


As i could see, at least in rails 1.8.6 its not part of the standard lib. Is it part from the ruby 1.9 lib? If not, should i install any gem?

Note that solutions using OpenSSL won't be accepted because it will fail anyway in "require 'digest/hmac'"

The code in question is here http://github.com/quetzall/cloud_cache/blob/master/lib/cloud_cache.rb

like image 804
VP. Avatar asked Dec 24 '09 19:12

VP.


2 Answers

It's available in 1.8.7. Try this:

ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9]

require 'openssl'
digest  = OpenSSL::Digest::Digest.new('sha1')
OpenSSL::HMAC.digest(digest, "superscret", "Lorem ipsum dolor sit amet")
OpenSSL::HMAC.hexdigest(digest, "superscret", "Lorem ipsum dolor sit amet")
like image 56
Chandra Patni Avatar answered Sep 17 '22 19:09

Chandra Patni


From the 1.9.3 docs:

CAUTION: Use of this library is discouraged, because this implementation was meant to be experimental but somehow got into the 1.9 series without being noticed. Please use OpenSSL::HMAC in the “openssl” library instead.

like image 45
Ash Berlin-Taylor Avatar answered Sep 18 '22 19:09

Ash Berlin-Taylor