How do I create a SHA1 hash in ruby?

2019-01-08 05:13发布

4条回答
疯言疯语
2楼-- · 2019-01-08 05:28

I created a helper gem which is a simple wrapper around some sha1 code

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

 => "4659d94e7082a65ca39e7b6725094f08a413250a" 

> "hello world".to_sha1

 => "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" 
查看更多
小情绪 Triste *
3楼-- · 2019-01-08 05:40

Where 'serialize' is some user function defined elsewhere.

 def generateKey(data)
    return Digest::SHA1.hexdigest ("#{serialize(data)}")
 end
查看更多
男人必须洒脱
4楼-- · 2019-01-08 05:41
require 'digest/sha1'
Digest::SHA1.hexdigest 'foo'
查看更多
爷、活的狠高调
5楼-- · 2019-01-08 05:43

For a Base64 encoded hash, to validated an Oauth signature, I used

require 'base64'
require 'hmac-sha1'

Base64.encode64((HMAC::SHA1.new('key') << 'base').digest).strip
查看更多
登录 后发表回答