Class: Base32::Random

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length = 16, padding = true) ⇒ Random

Returns a new instance of Random.



10
11
12
13
# File 'lib/base32/random.rb', line 10

def initialize(length = 16, padding = true)
  @length  = length
  @padding = padding
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



8
9
10
# File 'lib/base32/random.rb', line 8

def length
  @length
end

#paddingObject (readonly)

Returns the value of attribute padding.



8
9
10
# File 'lib/base32/random.rb', line 8

def padding
  @padding
end

Instance Method Details

#alphabetObject



24
25
26
# File 'lib/base32/random.rb', line 24

def alphabet
  @alphabet ||= Base32::Alphabet.new Base32::Alphabet::CHARS
end

#callObject



15
16
17
18
19
20
21
22
# File 'lib/base32/random.rb', line 15

def call
  random = ''

  OpenSSL::Random.random_bytes(length).each_byte do |b|
    random += alphabet.to_s[b % 32]
  end
  padding ? random.ljust((length / 8.0).ceil * 8, '=') : random
end