Module: ConcernsOnRails::Support::RandomValue

Defined in:
lib/concerns_on_rails/support/random_value.rb

Overview

Shared random-value generation used by Hashable (:custom) and Tokenizable (:alphanumeric / :numeric). Samples length characters uniformly from alphabet using SecureRandom.

Class Method Summary collapse

Class Method Details

.from_alphabet(alphabet, length) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/concerns_on_rails/support/random_value.rb', line 11

def from_alphabet(alphabet, length)
  size = alphabet.size
  # Alphabets wider than a byte can't use the batched path below.
  return Array.new(length) { alphabet[SecureRandom.random_number(size)] }.join if size > 256

  # One batched CSPRNG draw with rejection sampling (drop bytes from the
  # biased tail so the distribution stays uniform) instead of one
  # SecureRandom call + one 1-char String per character.
  limit = (256 / size) * size
  result = String.new(capacity: length)
  while result.length < length
    SecureRandom.bytes(length - result.length).each_byte do |byte|
      next if byte >= limit

      result << alphabet[byte % size]
      break if result.length == length
    end
  end
  result
end