Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created September 7, 2017 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnunemaker/87fb475129c0601fa72695afe912b02f to your computer and use it in GitHub Desktop.
Save jnunemaker/87fb475129c0601fa72695afe912b02f to your computer and use it in GitHub Desktop.
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