Class: PassForge::Pronounceable
- Inherits:
-
Object
- Object
- PassForge::Pronounceable
- Defined in:
- lib/passforge/pronounceable.rb
Overview
Pronounceable password generator Creates passwords that are easier to type and remember
Constant Summary collapse
- CONSONANTS =
%w[b c d f g h j k l m n p r s t v w x z].freeze
- VOWELS =
%w[a e i o u].freeze
Class Method Summary collapse
-
.generate(length: 12, capitalize: true, numbers: true, symbols: false) ⇒ String
Generate a pronounceable password.
-
.generate_pronounceable_string(length) ⇒ Object
Generate pronounceable string by alternating consonants and vowels.
Class Method Details
.generate(length: 12, capitalize: true, numbers: true, symbols: false) ⇒ String
Generate a pronounceable password
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/passforge/pronounceable.rb', line 24 def self.generate(length: 12, capitalize: true, numbers: true, symbols: false) raise ArgumentError, "Length must be at least 4" if length < 4 # Reserve space for numbers and symbols letter_count = length letter_count -= 2 if numbers letter_count -= 1 if symbols # Generate pronounceable base password = generate_pronounceable_string(letter_count) # Capitalize first letter password[0] = password[0].upcase if capitalize # Add numbers if numbers 2.times { password += SecureRandom.random_number(10).to_s } end # Add symbol if symbols symbol = Charsets::SYMBOLS.sample password += symbol end password end |
.generate_pronounceable_string(length) ⇒ Object
Generate pronounceable string by alternating consonants and vowels
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/passforge/pronounceable.rb', line 54 def self.generate_pronounceable_string(length) result = [] use_consonant = [true, false].sample length.times do if use_consonant result << CONSONANTS.sample else result << VOWELS.sample end use_consonant = !use_consonant end result.join end |