Skip to content

Instantly share code, notes, and snippets.

@peterc
Created August 9, 2014 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/737d9178f02118f8e315 to your computer and use it in GitHub Desktop.
Save peterc/737d9178f02118f8e315 to your computer and use it in GitHub Desktop.
strhash.rb
# 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