class SecureRandomString

Constants

CHARACTERS
LC_LETTERS
NUMBERS
UC_LETTERS

Public Class Methods

new(length, options = {}) click to toggle source
# File lib/secure_random_string.rb, line 10
def initialize(length, options = {})
  # By default, don't include characters in strings.
  options[:characters] ||= false

  # Build a set of possible characters for the string
  set = []
  set += UC_LETTERS       unless options[:uppercase] == false
  set += LC_LETTERS       unless options[:lowercase] == false
  set += NUMBERS          unless options[:numbers] == false
  set += CHARACTERS       unless options[:characters] == false
  set += options[:extra]  if options[:extra].is_a?(Array)

  # Populate the string
  length.times.each do
    self << set[SecureRandom.random_number(set.size)].to_s
  end
end