Class: Pwgen::Password

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

Overview

docs to go here

Constant Summary collapse

CONFUSABLE_CHARS =
['$', 'S', 's', '5', 'B', 'b', '8', '|', 'I', 'i', 'o', '0', '1', 'l', 'O', '!'].freeze
URL_UNSAFE =
['$', '&', '+', ',', '/', ':', ';', '=', '?', '@', '<', '>', '#', '%', '{', '}', '|', '^', '~', '[', ']', '\\', '"', '`'].freeze

Class Method Summary collapse

Class Method Details

.calculate_phonetic_password(password) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pwgen.rb', line 128

def self.calculate_phonetic_password(password)
    phonetic = ''

    password.split('').each do |letter|
        phonetic += if letter =~ /[[:digit:]]/
                        letter.to_i.to_words
                    elsif letter =~ /[[:upper:]]/
                        letter.to_s.phonetic_encode.upcase
                    elsif letter =~ /[[:lower:]]/
                        letter.to_s.phonetic_encode.downcase
                    else
                        format('(%<name>s)', name: letter.to_s.punctuation_name)
                    end
        phonetic += ' '
    end
    phonetic.rstrip
end

.configure(options) ⇒ Object



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
# File 'lib/pwgen.rb', line 46

def self.configure(options)
    $lower_case = []
    $lower_case = ('a'..'z').to_a unless options[:skip_lower_case]
    $lower_case -= CONFUSABLE_CHARS if options[:skip_confusables]

    $upper_case = []
    $upper_case = ('A'..'Z').to_a unless options[:skip_upper_case]
    $upper_case -= CONFUSABLE_CHARS if options[:skip_confusables]

    $digits = []
    $digits = ('0'..'9').to_a unless options[:skip_digits]
    $digits -= CONFUSABLE_CHARS if options[:skip_confusables]

    $symbols = []
    $symbols = ['!', '@', '#', '£', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', '[', ']', ':', ';', '"', '\'', '|', '\\', '~', '`', '<', '>', '?', ',', '.', '/', ' '].freeze unless options[:skip_symbols]
    $symbols -= CONFUSABLE_CHARS if options[:skip_confusables]
    $symbols -= URL_UNSAFE if options[:skip_url_unsafe]

    $alpha = []
    $alpha = $lower_case + $upper_case

    $alphanum = []
    $alphanum = $lower_case + $upper_case + $digits

    $full_character_set = []
    $full_character_set = $alphanum + $symbols

    raise 'You messed up!' if $full_character_set.empty?
end

.generate_password(length = 12, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pwgen.rb', line 91

def self.generate_password(length = 12, options = {})
    raise 'length must be a number!' unless length !~ /\D/

    length = length.to_i

    raise 'length must be a positive number!' unless length.positive?

    if options[:digits] || options[:symbols]
        raise 'length is to short to meet digits/symbols requirements!' if length < options[:digits].to_i + options[:symbols].to_i
    end

    reset(options)

    shuffle = false

    if options[:digits]
        digits = options[:digits].to_i

        $password += (1..digits).collect { $digits[rand($digits.size)] }.join
        length -= digits
        shuffle = true
    end

    if options[:symbols]
        symbols = options[:symbols].to_i

        $password += (1..symbols).collect { $symbols[rand($symbols.size)] }.join
        length -= symbols
        shuffle = true
    end

    $password += (1..length).collect { $full_character_set[rand($full_character_set.size)] }.join
    $password = $password.split('').shuffle.join if shuffle

    $phonetic_password = calculate_phonetic_password($password) unless options[:skip_phonetic]
end

.passwordObject



31
32
33
# File 'lib/pwgen.rb', line 31

def self.password
    $password
end

.phonetic_passwordObject



35
36
37
# File 'lib/pwgen.rb', line 35

def self.phonetic_password
    $phonetic_password
end

.reset(options = nil) ⇒ Object



39
40
41
42
43
44
# File 'lib/pwgen.rb', line 39

def self.reset(options = nil)
    $password = ''
    $phonetic_password = ''

    configure(options) unless options.nil?
end

.show_configurationObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pwgen.rb', line 76

def self.show_configuration
    puts "Lower case: #{$lower_case}"
    puts "Upper case: #{$upper_case}"
    puts "Digits: #{$digits}"
    puts "Symbols: #{$symbols}"

    puts "Alpha: #{$alpha}"
    puts "AlphaNumeric: #{$alphanum}"

    puts "Confusable Chars: #{CONFUSABLE_CHARS}"
    puts "URL Unsafe: #{URL_UNSAFE}"

    puts "Full Character Set: #{$full_character_set}"
end