Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sha256 hexdigest to number for use in mapping a string to a partition number (not consistent hashing, but just in simple hash => partition)
irb(main):001:0> require "digest"
=> true
irb(main):002:0> Digest::SHA256.hexdigest("asdf")
=> "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
irb(main):003:0> Digest::SHA256.hexdigest("asdf").to_i(16)
=> 108959270400061671294053818573968651411470832267186275529291850190552309358907
irb(main):004:0> Digest::SHA256.hexdigest("asdf").to_i(16).to_s(16)
=> "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"
irb(main):005:0> Digest::SHA256.hexdigest("asdf").to_i(16) % 10
=> 7
irb(main):006:0> Digest::SHA256.hexdigest("asdf").to_i(16) % 10
=> 7
irb(main):008:0> Digest::SHA256.hexdigest("asdf").to_i(16) % 10
=> 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.