Created
April 9, 2012 12:28
-
-
Save DavidJRobertson/2343131 to your computer and use it in GitHub Desktop.
Hash function for http://class.coursera.org/crypto
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
class String | |
def last(n) | |
self[-n,n] | |
end | |
end | |
require "digest" | |
def hash_message(message) | |
sha256 = Digest::SHA2.new(256) | |
sha256hash = sha256.digest message | |
binstring = "" | |
sha256hash.bytes.each do |byte| | |
binval = byte.to_s(2) | |
if binval.length < 8 | |
binval = '0' * (8 - binval.length) + binval | |
end | |
binstring << binval | |
end | |
return binstring.last(50).to_i(2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment