class EasyPasswords::Generator
Public Class Methods
new(separators = EasyPasswords::SEPARATORS)
click to toggle source
# File lib/easy_passwords/easy_passwords.rb, line 53 def initialize(separators = EasyPasswords::SEPARATORS) @@wordlist_size = @@wordlist.length @@separators = separators @@separators_size = @@separators.length @rand = SecureRandom end
Public Instance Methods
generate(max_length = EasyPasswords::DEFAULT_MAX_LENGTH)
click to toggle source
Public: Returns a random generated password string.
max_length - max number of characters used in password, it could generate password shorter by 3 characters.
Example
generate 8 # => "Fun_Crop" generate # => "spate7Coup"
Returns a password string.
# File lib/easy_passwords/easy_passwords.rb, line 73 def generate(max_length = EasyPasswords::DEFAULT_MAX_LENGTH) fail "Password minimal length is #{EasyPasswords::MIN_WORD_LENGTH}" if max_length < EasyPasswords::MIN_WORD_LENGTH output = '' while output.size < (max_length - (EasyPasswords::MIN_WORD_LENGTH - 1)) add_part(output, max_length) end output end
Private Instance Methods
add_part(output, max_length)
click to toggle source
# File lib/easy_passwords/easy_passwords.rb, line 106 def add_part(output, max_length) output << random_word(choose_word_length(max_length, output.size)) output << @@separators[@rand.random_number(@@separators_size)] if output.size < max_length end
choose_word_length(max_length, pass_size)
click to toggle source
# File lib/easy_passwords/easy_passwords.rb, line 84 def choose_word_length(max_length, pass_size) if pass_size == 0 if max_length < (EasyPasswords::MIN_WORD_LENGTH * 2 + 1) max_length else max_length / 2 end else max_length - pass_size end end
random_word(maxsize = EasyPasswords::MAX_WORD_LENGTH)
click to toggle source
# File lib/easy_passwords/easy_passwords.rb, line 96 def random_word(maxsize = EasyPasswords::MAX_WORD_LENGTH) list = if maxsize < EasyPasswords::MAX_WORD_LENGTH @@wordlist.select { |w| w.size <= maxsize } else @@wordlist end word = list[@rand.random_number(list.size)] @rand.random_number(2) == 0 ? word.capitalize : word end