Skip to content

Instantly share code, notes, and snippets.

@jfcalvo
Created October 3, 2011 09:28
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jfcalvo/1258775 to your computer and use it in GitHub Desktop.
Save jfcalvo/1258775 to your computer and use it in GitHub Desktop.
Hash of files an strings with Ruby using MD5 and SHA256
require 'digest'
# Get SHA256 Hash of a file
puts Digest::SHA256.hexdigest File.read "data.dat"
# Get MD5 Hash of a file
puts Digest::MD5.hexdigest File.read "data.dat"
# Get MD5 Hash of a string
puts Digest::SHA256.hexdigest "Hello World"
# Get SHA256 Hash of a string using update
sha256 = Digest::SHA256.new
sha256.update "Hello"
sha256.update " World"
puts sha256.hexdigest
@karaaie
Copy link

karaaie commented Dec 2, 2014

Copy paste error on line 7 buddy 😄 but thanks for the example!

@rdp
Copy link

rdp commented Jan 29, 2016

also for md5 a file: Digest::MD5.file('filename').hexdigest

@bugoverfl0w
Copy link

cool

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment