Class: RightSupport::Data::Token
- Defined in:
- lib/right_support/data/token.rb
Overview
unique token generator. short tokens that are friendler than raw UUIDs for logging purposes, etc.
Constant Summary collapse
- TOKEN_LENGTH =
exact length of all generated tokens.
13
Class Method Summary collapse
-
.generate ⇒ String
Generate new token.
Class Method Details
.generate ⇒ String
Generate new token
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/right_support/data/token.rb', line 37 def self.generate # MD5 is 128bit = 25 chars in base 36, need to fit in less # The code below produces a 64 bit value evenly distributed. i = ::Digest::MD5.hexdigest(::RightSupport::Data::UUID.generate).to_i(16) t = ((i >> 64) ^ (i & 0xFFFFFFFFFFFFFFFF)).to_s(36) # left-pad with zeros to keep token length consistant. if t.length < TOKEN_LENGTH t = '0' * (TOKEN_LENGTH - t.length) + t end t end |