class Pwgen::Password

docs to go here

Constants

CONFUSABLE_CHARS
URL_UNSAFE

Public Class Methods

calculate_phonetic_password(password) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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
password() click to toggle source
# File lib/pwgen.rb, line 31
def self.password
    $password
end
phonetic_password() click to toggle source
# File lib/pwgen.rb, line 35
def self.phonetic_password
    $phonetic_password
end
reset(options = nil) click to toggle source
# File lib/pwgen.rb, line 39
def self.reset(options = nil)
    $password = ''
    $phonetic_password = ''

    configure(options) unless options.nil?
end
show_configuration() click to toggle source
# 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