Class: DevRandomPasswords::Generator
- Inherits:
-
Object
- Object
- DevRandomPasswords::Generator
- Defined in:
- lib/dev-random-passwords.rb
Constant Summary collapse
- LOWERCASE_CHARS =
('a'..'z').to_a.join("")
- UPPERCASE_CHARS =
('A'..'Z').to_a.join("")
- DIGITS =
('0'..'9').to_a.join("")
- SPECIAL_CHARS =
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
- NOT_READABLE_ERROR =
"is not readable by the current user please change permissions on this file or switch users."
- REQUIREMENT_CONFLICT_ERROR =
"You required a character type that is not possible with your current password options. Set your options and try again."
Instance Attribute Summary collapse
-
#char_length ⇒ Object
Returns the value of attribute char_length.
-
#charset ⇒ Object
Returns the value of attribute charset.
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize ⇒ Generator
constructor
A new instance of Generator.
- #set_options(options = { 'lowercase' => true, 'uppercase' => true, 'digits' => true, 'special' => true, 'include' => nil, 'exclude' => nil, 'length' => 12, 'requirements' => nil}) ⇒ Object
Constructor Details
#initialize ⇒ Generator
Returns a new instance of Generator.
14 15 16 17 18 19 |
# File 'lib/dev-random-passwords.rb', line 14 def initialize @charset = LOWERCASE_CHARS + UPPERCASE_CHARS + DIGITS + SPECIAL_CHARS @char_length = 12 @requirements = nil @hardware_random = false end |
Instance Attribute Details
#char_length ⇒ Object
Returns the value of attribute char_length.
5 6 7 |
# File 'lib/dev-random-passwords.rb', line 5 def char_length @char_length end |
#charset ⇒ Object
Returns the value of attribute charset.
4 5 6 |
# File 'lib/dev-random-passwords.rb', line 4 def charset @charset end |
Instance Method Details
#generate ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/dev-random-passwords.rb', line 116 def generate new_password = "" @char_length.times do rand_num = get_byte while rand_num >= @charset.length rand_num -= @charset.length end new_password += @charset[rand_num] end check_for_conflicts if requirements_met? new_password return new_password else return generate end end |
#set_options(options = { 'lowercase' => true, 'uppercase' => true, 'digits' => true, 'special' => true, 'include' => nil, 'exclude' => nil, 'length' => 12, 'requirements' => nil}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/dev-random-passwords.rb', line 21 def (={ 'lowercase' => true, 'uppercase' => true, 'digits' => true, 'special' => true, 'include' => nil, 'exclude' => nil, 'length' => 12, 'requirements' => nil}) new_set = "" if ['lowercase'] new_set += LOWERCASE_CHARS end if ['uppercase'] new_set += UPPERCASE_CHARS end if ['digits'] new_set += DIGITS end if ['special'] new_set += SPECIAL_CHARS end if new_set.empty? unless ['lowercase'] == false new_set = new_set + LOWERCASE_CHARS end unless ['uppercase'] == false new_set = new_set + UPPERCASE_CHARS end unless ['digits'] == false new_set = new_set + DIGITS end unless ['special'] == false new_set = new_set + SPECIAL_CHARS end end if ['include'] if ['include'].respond_to? :each ['include'].each do |char| unless new_set.include? char new_set.insert(-1, char) end end elsif ['include'].respond_to? :split ['include'].split("").each do |char| unless new_set.include? char new_set.insert(-1, char) end end end end if ['exclude'] if ['exclude'].respond_to? :each ['exclude'].each do |char| if new_set.include? char new_set = new_set.tr(char, '') end end elsif ['exclude'].respond_to? :split ['exclude'].split("").each do |char| if new_set.include? char new_set = new_set.tr(char, '') end end end end if new_set.empty? raise BAD_CHARACTER_SET_ERROR end @charset = new_set if ['length'] if ['length'].respond_to? :to_i @char_length = ['length'].to_i end end if ['requirements'] @requirements = ['requirements'] end nil end |