Created
February 14, 2012 04:50
-
-
Save ndemonner/1823635 to your computer and use it in GitHub Desktop.
Hashing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# we need access to the digest library for proper hashing | |
require 'digest' | |
# first we create the object that will do the hashing and store it in the hasher variable | |
hasher = Digest::SHA256.new | |
# next we ask for their password | |
puts "What's your password?" | |
password = gets.chomp | |
# we hash their password and store it in the hash variable | |
hash = hasher.digest(password) | |
# now we erase their password to show that all we need is the hash | |
password = "" | |
# next we ask them for their password again | |
puts "Remember your password?" | |
password = gets.chomp | |
# if the old hash and the new hash are the same, they've got the right password! | |
if hasher.digest(password) == hash | |
puts "Yes, that's your password!" | |
else | |
puts "Nope, you got it wrong!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment