Class: Password

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-password-gen.rb,
lib/simple-password-gen/version.rb

Overview

A simple Password generator. Central parts are based on snippets.dzone.com/posts/show/2137 and comments.

Constant Summary collapse

CONSONANTS =

consonantes and pronounceable combinations with vowels

%w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sch sh sl sp st th tr)
VOWELS =

vowels and sound-alike y

%w(a e i o u y)
CHARS =

some characters

(('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + "%&/()[]!\"§$,.-;:_#'+*?".split(//u)) - "io01lO".split(//u)
MAJOR =

:nodoc:

0
MINOR =

:nodoc:

1
PATCH =

:nodoc:

5
VERSION =

:nodoc:

[MAJOR, MINOR, PATCH].join '.'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(len = (8..12)) ⇒ Password

Creates a new password generator. The length len might be an Integer or a Range.



15
16
17
18
19
20
21
22
23
# File 'lib/simple-password-gen.rb', line 15

def initialize len = (8..12)
  if len.is_a? Integer
    @length = Range.new(len-1, len+1)
  elsif len.is_a? Range
    @length = len
  else
    raise ArgumentException, "Length is neither an Integer nor a Range."
  end
end

Class Method Details

.pronounceable(len = (8..12)) ⇒ Object

Similar to #pronounceable, this generates a pronounceable password.



27
28
29
# File 'lib/simple-password-gen.rb', line 27

def pronounceable len = (8..12)
  self.new(len).pronounceable
end

.random(len = (8..12)) ⇒ Object

Similar to #random, this generates a random password.



32
33
34
# File 'lib/simple-password-gen.rb', line 32

def random len = (8..12)
  self.new(len).random
end

Instance Method Details

#pronounceableObject

Generates a pronounceable password.



38
39
40
41
42
43
44
45
46
# File 'lib/simple-password-gen.rb', line 38

def pronounceable
  size = @length.to_a[rand(@length.count)] - 2
  f, pw = true, ''
  size.times do
    pw << (f ? CONSONANTS[rand * CONSONANTS.size] : VOWELS[rand * VOWELS.size])
    f = !f
  end
  pw
end

#randomObject

Unlike #pronounceable, this does not ensure the pronounceability and will include some special characters, but will exclude “unfriendly” characters (like 0, O).



51
52
53
54
55
56
# File 'lib/simple-password-gen.rb', line 51

def random
  size = @length.to_a[rand(@length.count)]
  (1..size).collect do |a|
    CHARS[rand(CHARS.size)]
  end.join ''
end