Class: Cuid2::Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/cuid2.rb

Overview

Hash is a class that generates a hash of a string

Constant Summary collapse

BIG_LENGTH =
32

Class Method Summary collapse

Class Method Details

.create(input, length = BIG_LENGTH) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cuid2.rb', line 28

def self.create(input, length = BIG_LENGTH)
  # The salt should be long enough to be globally unique across the full
  # length of the hash. For simplicity, we use the same length as the
  # intended id output, defaulting to the maximum recommended size.
  salt = Entropy.create(length)
  text = input + salt

  # Drop the first two characters because they bias the histogram
  # to the left.
  Digest::SHA512.hexdigest(text).chars.map(&:to_i).join.to_i.to_s(36)[2..]
end