Class: DevRandomPasswords::Generator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeGenerator

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_lengthObject

Returns the value of attribute char_length.



5
6
7
# File 'lib/dev-random-passwords.rb', line 5

def char_length
  @char_length
end

#charsetObject

Returns the value of attribute charset.



4
5
6
# File 'lib/dev-random-passwords.rb', line 4

def charset
  @charset
end

Instance Method Details

#generateObject



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 set_options(options={
  'lowercase' => true,
  'uppercase' => true,
  'digits' => true,
  'special' => true,
  'include' => nil,
  'exclude' => nil,
  'length' => 12,
  'requirements' => nil})

  new_set = ""

  if options['lowercase']
    new_set += LOWERCASE_CHARS
  end

  if options['uppercase']
    new_set += UPPERCASE_CHARS
  end

  if options['digits']
    new_set += DIGITS
  end

  if options['special']
    new_set += SPECIAL_CHARS
  end

  if new_set.empty?
    unless options['lowercase'] == false
      new_set = new_set + LOWERCASE_CHARS
    end
    unless options['uppercase'] == false
      new_set = new_set + UPPERCASE_CHARS
    end
    unless options['digits'] == false
      new_set = new_set + DIGITS
    end
    unless options['special'] == false
      new_set = new_set + SPECIAL_CHARS
    end
  end

  if options['include']
    if options['include'].respond_to? :each
      options['include'].each do |char|
        unless new_set.include? char
          new_set.insert(-1, char)
        end
      end
    elsif options['include'].respond_to? :split
      options['include'].split("").each do |char|
        unless new_set.include? char
          new_set.insert(-1, char)
        end
      end
    end
  end

  if options['exclude']
    if options['exclude'].respond_to? :each
      options['exclude'].each do |char|
        if new_set.include? char
          new_set = new_set.tr(char, '')
        end
      end
    elsif options['exclude'].respond_to? :split
      options['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 options['length']
    if options['length'].respond_to? :to_i
      @char_length = options['length'].to_i
    end
  end

  if options['requirements']
    @requirements = options['requirements']
  end

  nil

end