Class: Tins::Token
Constant Summary collapse
- DEFAULT_ALPHABET =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".freeze
- BASE64_ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".freeze
- BASE64_URL_FILENAME_SAFE_ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_".freeze
- BASE32_ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".freeze
- BASE32_EXTENDED_HEX_ALPHABET =
"0123456789ABCDEFGHIJKLMNOPQRSTUV".freeze
- BASE16_UPPERCASE_ALPHABET =
"0123456789ABCDEF".freeze
- BASE16_LOWERCASE_ALPHABET =
"0123456789abcdef".freeze
- BASE16_ALPHABET =
BASE16_UPPERCASE_ALPHABET
Instance Attribute Summary collapse
-
#bits ⇒ Object
Returns the value of attribute bits.
Instance Method Summary collapse
-
#initialize(bits: 128, length: nil, alphabet: DEFAULT_ALPHABET, random: SecureRandom) ⇒ Token
constructor
A new instance of Token.
Constructor Details
#initialize(bits: 128, length: nil, alphabet: DEFAULT_ALPHABET, random: SecureRandom) ⇒ Token
Returns a new instance of Token.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tins/token.rb', line 24 def initialize(bits: 128, length: nil, alphabet: DEFAULT_ALPHABET, random: SecureRandom) alphabet.size > 1 or raise ArgumentError, 'need at least 2 symbols in alphabet' if length length > 0 or raise ArgumentError, 'length has to be positive' else bits > 0 or raise ArgumentError, 'bits has to be positive' length = (Math.log(1 << bits) / Math.log(alphabet.size)).ceil end self.bits = (Math.log(alphabet.size ** length) / Math.log(2)).floor token = '' length.times { token << alphabet[random.random_number(alphabet.size)] } super token end |
Instance Attribute Details
#bits ⇒ Object
Returns the value of attribute bits.
38 39 40 |
# File 'lib/tins/token.rb', line 38 def bits @bits end |