Class: Cryptophysh::PasswordGenerator

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

Constant Summary collapse

DEFAULT_LENGTH =
12
DEFAULT_UPPERCASE =
true
DEFAULT_LOWERCASE =
true
DEFAULT_NUMBER_COUNT =
3
DEFAULT_SPECIAL_COUNT =
3
SPECIAL_CHARACTERS =
%w[@ % ! ? * ^ &].freeze

Instance Method Summary collapse

Constructor Details

#initialize(length: DEFAULT_LENGTH, uppercase: DEFAULT_UPPERCASE, lowercase: DEFAULT_LOWERCASE, number: DEFAULT_NUMBER_COUNT, special: DEFAULT_SPECIAL_COUNT) ⇒ Cryptophysh::PasswordGenerator

Instantiatates a new PasswordGenerator instance.

Parameters:

  • length (Integer) (defaults to: DEFAULT_LENGTH)

    the total length of any password generated with the #generate_password method

  • uppercase (true, false) (defaults to: DEFAULT_UPPERCASE)

    include one or more uppercase ‘A-Z` characters

  • lowercase (true, false) (defaults to: DEFAULT_LOWERCASE)

    include one or more lowercase ‘a-z` characters

  • number (Integer) (defaults to: DEFAULT_NUMBER_COUNT)

    exact number of numeric ‘0-9- characters

  • special (Integer) (defaults to: DEFAULT_SPECIAL_COUNT)

    exact number of special ‘@%!?*^&` characters



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cryptophysh/password_generator.rb', line 22

def initialize(
  length: DEFAULT_LENGTH,
  uppercase: DEFAULT_UPPERCASE,
  lowercase: DEFAULT_LOWERCASE,
  number: DEFAULT_NUMBER_COUNT,
  special: DEFAULT_SPECIAL_COUNT
)
  @length = length
  @uppercase = uppercase
  @lowercase = lowercase
  @number = number
  @special = special

  validate_attributes
end

Instance Method Details

#generate_passwordString

Returns the generated password of @length characters.

Returns:

  • (String)

    the generated password of @length characters



39
40
41
42
43
44
45
# File 'lib/cryptophysh/password_generator.rb', line 39

def generate_password
  password = ""
  @special.times { password += SPECIAL_CHARACTERS.sample }
  password += number_string if @number > 0
  password += character_string(@length - password.size)
  password.chars.shuffle.join
end