How do I create a SHA1 hash in ruby?

RubyHashSha1

Ruby Problem Overview


Ruby Solutions


Solution 1 - Ruby

require 'digest/sha1'
Digest::SHA1.hexdigest 'foo'

Solution 2 - Ruby

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

Solution 3 - Ruby

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" 

Solution 4 - Ruby

Where 'serialize' is some user function defined elsewhere.

 def generateKey(data)
	return Digest::SHA1.hexdigest ("#{serialize(data)}")
 end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionquackingduckView Question on Stackoverflow
Solution 1 - RubydevstopfixView Answer on Stackoverflow
Solution 2 - RubyThiago GanzarolliView Answer on Stackoverflow
Solution 3 - RubyGregory OstermayrView Answer on Stackoverflow
Solution 4 - Rubyr4ccoonView Answer on Stackoverflow