Created
August 9, 2014 15:22
-
-
Save peterc/737d9178f02118f8e315 to your computer and use it in GitHub Desktop.
strhash.rb
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
# not for production, obviously | |
def hash(str, length_of_hash = 20) | |
added = str.codepoints.map(&:ord).each_slice(length_of_hash).inject do |a, b| | |
a.map.with_index { |c, d| (c + (b[d] || 0)) % 256 } | |
end | |
added.map { |val| val.to_s(16) }.join | |
end | |
p hash("Hello world!", 8) # => "bad1d08d6f20776f" | |
p hash("Hello worlds!", 8) # => "bad1d0df9020776f" | |
p hash("Bello world", 8) # => "b4d1d06c6f20776f" | |
p hash("Badminton!!", 8) # => "b082856d696e746f" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment