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 a DZone snippet and its comments: web.archive.org/web/20090204082442/http://snippets.dzone.com/posts/show/2137

Defined Under Namespace

Modules: CharacterSets

Constant Summary collapse

DEFAULT_LENGTH =

default length range value for new password generator instances

8..12
VERSION =
"0.2.0.alpha"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(len = DEFAULT_LENGTH) ⇒ Password

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



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

def initialize(len = DEFAULT_LENGTH)
  coerce_length! len
end

Instance Attribute Details

#lengthObject (readonly)

the possible length values



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

def length
  @length
end

Class Method Details

.pronounceable(len = DEFAULT_LENGTH, visual_safe: false) ⇒ Object

Short-hand for #new.pronouncable.



37
38
39
# File 'lib/simple-password-gen.rb', line 37

def pronounceable(len = DEFAULT_LENGTH, visual_safe: false)
  new(len).pronounceable(visual_safe: visual_safe)
end

.random(len = DEFAULT_LENGTH) ⇒ Object

Short-hand for #new.random.



42
43
44
# File 'lib/simple-password-gen.rb', line 42

def random(len = DEFAULT_LENGTH)
  new(len).random
end

.urlsafe(len = DEFAULT_LENGTH) ⇒ Object

Short-hand for #new.urlsafe.



47
48
49
# File 'lib/simple-password-gen.rb', line 47

def urlsafe(len = DEFAULT_LENGTH)
  new(len).urlsafe
end

Instance Method Details

#pronounceable(visual_safe:) ⇒ Object

Generates a pronounceable password.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/simple-password-gen.rb', line 53

def pronounceable(visual_safe:)
  set = rand > 0.5

  consonants = if visual_safe
                 CharacterSets::VISUAL_SAFE_CONSONANTS
               else
                 CharacterSets::CONSONANTS
               end

  vowels = if visual_safe
             CharacterSets::VISUAL_SAFE_VOWELS
           else
             CharacterSets::VOWELS
           end

  build_password {
    set = !set
    set ? consonants.sample : vowels.sample
  }
end

#randomObject

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



77
78
79
# File 'lib/simple-password-gen.rb', line 77

def random
  build_password { CharacterSets::ALL_CHARS.sample }
end

#urlsafeObject

Generates a passwort suitable for usage in HTTP(S) URLs, i.e. the following characters are excluded: :@/.



83
84
85
# File 'lib/simple-password-gen.rb', line 83

def urlsafe
  build_password { CharacterSets::URL_SAFE.sample }
end