¿Cómo puedo crear un hash SHA1 en ruby?


 152
Author: quackingduck, 2008-08-29

4 answers

require 'digest/sha1'
Digest::SHA1.hexdigest 'foo'
 369
Author: devstopfix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-29 15:56:48

Para un hash codificado en Base64, para validar una firma Oauth, usé

require 'base64'
require 'hmac-sha1'

Base64.encode64((HMAC::SHA1.new('key') << 'base').digest).strip
 8
Author: Thiago Ganzarolli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-11 20:42:30

He creado un helper gem que es un simple envoltorio alrededor de algún código sha1

require 'rickshaw'
> Rickshaw::SHA1.hash('LICENSE.txt')

 => "4659d94e7082a65ca39e7b6725094f08a413250a" 

> "hello world".to_sha1

 => "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" 
 6
Author: Gregory Ostermayr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-06 17:51:39

Donde 'serializar' es alguna función de usuario definida en otro lugar.

 def generateKey(data)
    return Digest::SHA1.hexdigest ("#{serialize(data)}")
 end
 -25
Author: r4ccoon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-26 05:12:04