Module: HumanToken

Defined in:
lib/human_token.rb,
lib/human_token/version.rb

Constant Summary collapse

DEFAULTS =
{ bytes: 16, base: BaseX::Base30L }
BASES =
[
  [:hex,         BaseX::Base16L],
  [:base_30,     BaseX::Base30L],
  [:base_31,     BaseX::Base31L],
  [:base_32,     BaseX::CrockfordBase32],
  [:base_58,     BaseX::BitcoinBase58],
  [:new_base_60, BaseX::NewBase60],
  [:base_62,     BaseX::Base62DUL],
]
VERSION =
"0.9.0"

Class Method Summary collapse

Class Method Details

.generate(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/human_token.rb', line 17

def self.generate(*args)
  options = normalize_generate_options(args, DEFAULTS)
  base, bytes, exact_bytes = options[:base], options[:bytes], options[:exact_bytes]

  base = BaseX.new(options[:characters]) if options[:characters]

  if exact_bytes
    base.encode(SecureRandom.random_bytes(exact_bytes))
  else
    length = (Math.log(256)/Math.log(base.base)*bytes).ceil
    token  = base.encode(SecureRandom.random_bytes(bytes + 1))
    token[-length..-1]
  end
end

.samples(*args) ⇒ Object



39
40
41
# File 'lib/human_token.rb', line 39

def self.samples(*args)
  STDERR.puts samples_string(*args)
end