Class: Passr::Generator

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

Overview

Handles account/ username creation and respective password generation

Class Method Summary collapse

Class Method Details

.is_all_unique_characters?(password) ⇒ Boolean

Determine if password has all unique characters

Parameters:

  • password (String)

    generated password

Returns:

  • (Boolean)

    true if password has all unique characters



69
70
71
72
73
74
75
76
77
78
# File 'lib/passr/generator.rb', line 69

def self.is_all_unique_characters?(password)
  password.length.times do |i|
    password.chars.each_with_index do |char, index|
      unless i == index
        return false if password[i] == char
      end
    end
  end
  true
end

.is_sanitary?(password) ⇒ Boolean

Determine if password meets all security parameters

Parameters:

  • password (String)

    generated password

Returns:

  • (Boolean)

    true if password meets are required security measures



56
57
58
59
60
61
62
# File 'lib/passr/generator.rb', line 56

def self.is_sanitary?(password)
  password.scan(/[!@#$%^&*()]/).count > 0 &&
  password.scan(/[0-9]/).count > 0 &&
  password.scan(/[A-Z]/).count > 0 &&
  password.scan(/[a-z]/).count > 0 &&
  is_all_unique_characters?(password)
end

.new_character(password) ⇒ String

Get a unique character that is not in current password

Parameters:

  • password (String)

    generated password

Returns:

  • (String)

    unique character that is not present in password



43
44
45
46
47
48
49
50
# File 'lib/passr/generator.rb', line 43

def self.new_character(password)
  CHARACTERS.each do |set|
    set.each do |char|
      return char unless password.include?(char)
      return char.upcase unless password.include?(char.upcase)
    end
  end
end

.password(length = nil) ⇒ String

Generates password of given length

Parameters:

  • length (Integer) (defaults to: nil)

    length of characters for generated password

Returns:

  • (String)

    generated password of give length



13
14
15
16
17
# File 'lib/passr/generator.rb', line 13

def self.password(length = nil)
  length = 15 if length.nil? || length.to_i > 40
  generated = Array.new(length).map { random_character }.join('')
  sanitize(generated)
end

.random_characterString

Returns a random character from the constant CHARACTERS.

Returns:

  • (String)

    a random character from the constant CHARACTERS



34
35
36
37
# File 'lib/passr/generator.rb', line 34

def self.random_character
  character_set = CHARACTERS[rand(3)]
  character_set[rand(character_set.length)]
end

.sanitize(password) ⇒ String

Sanitize password to ensure there are all unique characters and there is at least one special character, capital letter, lowercase letter, and number

Parameters:

  • password (String)

    pre-sanitized/ initially generated password

Returns:

  • (String)

    sanitized and completely unique password



24
25
26
27
28
29
30
31
# File 'lib/passr/generator.rb', line 24

def self.sanitize(password)
  password.add_special!(CHARACTERS[0]) if password.scan(/[!@#$%^&*()]/).empty?
  password.add_number!(CHARACTERS[1]) if password.scan(/[0-9]/).empty?
  password.add_capital_letter!(CHARACTERS[2]) if password.scan(/[A-Z]/).empty?
  password.unique_chars!
  sanitize(password) unless is_sanitary?(password)
  return password
end