Class: PasswordGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/lockr/pwdgen.rb

Constant Summary collapse

ALPHA =
('A'..'Z').to_a|('a'..'z').to_a
ALPHA_NUM =
('A'..'Z').to_a|('a'..'z').to_a|('0'..'9').to_a
ALPHA_NUM_SYM =
(' '..'!').to_a|('#'..'[').to_a|(']'..'~').to_a

Instance Method Summary collapse

Instance Method Details

#generate(params) ⇒ Object

generate a random password. format of parameter: rxx - r = type (a … alpha, n … alpha + num, s … alpha + num + sym) and xx = password length

example: n10 -> GcQfP4OBFh a12 -> DChpRnhpHiha s18 -> JHnXZ<hrcVKorO6mO:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lockr/pwdgen.rb', line 17

def generate( params)
  pwd = []
  range = nil
  
  case params[0]
  when 's'
    range = ALPHA_NUM_SYM
  when 'n'
    range = ALPHA_NUM
  when 'a'
    range = ALPHA
  else
    puts 'Invalid parameter: ' + params
    exit 50
  end
  
  # create x random characters
  params[1..params.length].to_i.times do
    l = SecureRandom.random_number(range.length)
    pwd << range[l]
  end
  
  pwd = pwd.join
  # print pwd with single quote escape
  say( "<%= color( 'Generated password: #{pwd.gsub( "'", "\\\\'")}', :yellow) %>")
  pwd
end