Class: Tokogen::Generator

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

Defined Under Namespace

Classes: AssertionFail

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(randomness_source:, alphabet:) ⇒ Generator

Returns a new instance of Generator.



8
9
10
11
12
13
14
15
# File 'lib/tokogen/generator.rb', line 8

def initialize(randomness_source:, alphabet:)
  @randomness_source = randomness_source
  @alphabet = alphabet

  @alphabet_size = @alphabet.size
  @max_char_index = @alphabet_size - 1
  @bits_per_char = @max_char_index.bit_length
end

Instance Attribute Details

#alphabetObject (readonly)

Returns the value of attribute alphabet.



6
7
8
# File 'lib/tokogen/generator.rb', line 6

def alphabet
  @alphabet
end

#randomness_sourceObject (readonly)

Returns the value of attribute randomness_source.



6
7
8
# File 'lib/tokogen/generator.rb', line 6

def randomness_source
  @randomness_source
end

Instance Method Details

#alphabet_char(index) ⇒ Object



47
48
49
# File 'lib/tokogen/generator.rb', line 47

def alphabet_char(index)
  @alphabet[index]
end

#generate(length) ⇒ Object

rubocop:disable Metrics/AbcSize

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tokogen/generator.rb', line 17

def generate(length) # rubocop:disable Metrics/AbcSize
  token_bits_amount = length * @bits_per_char
  bytes_to_read = full_bytes_in_bits(token_bits_amount)
  bytes = random_bytes(bytes_to_read)
  splitter = BitSplitter.new(bytes.each_byte)
  combiner = BitCombiner.new(splitter.each, @bits_per_char)
  # It's possible we've read a couple exta bits of randomness,
  # since randomness is rounded to bytes.
  # Here we only take first `length` of bit that we need.
  indexes = combiner.each.take(length)
  raise AssertionFail, 'Invalid length' if indexes.size != length
  indexes.map do |index|
    # We split out random data into chunks of bits with fixed length.
    # Therefore it's possible to have an index value that is larger than
    # an alphabet size.
    # In this case we'd resolve to nil, so we're just using modulo of the
    # alphabet size. This will probably ruin the distribution that
    # the randromness source provides, but it will at least work.
    # If you don't want this behavior, just ensure you're using an alphabet
    # with an even size - then there will always be a bijection between
    # the generated indicies and the alphabet and the described issue
    # will never occur.
    alphabet_char(index % @alphabet_size)
  end.join
end

#random_bytes(size) ⇒ Object



43
44
45
# File 'lib/tokogen/generator.rb', line 43

def random_bytes(size)
  @randomness_source.random_bytes(size)
end