Class: PwGen
- Inherits:
-
Object
- Object
- PwGen
- Defined in:
- lib/ppg/pwgen.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#phonemes ⇒ Object
writeonly
Sets the attribute phonemes.
Instance Method Summary collapse
- #add_mutator(count, proc) ⇒ Object
- #fill_in_password_pattern! ⇒ Object
- #generate_password ⇒ Object
- #generate_password_pattern! ⇒ Object
-
#initialize(length, phonemes = nil) ⇒ PwGen
constructor
A new instance of PwGen.
- #mutate! ⇒ Object
Constructor Details
#initialize(length, phonemes = nil) ⇒ PwGen
Returns a new instance of PwGen.
13 14 15 16 17 18 19 20 |
# File 'lib/ppg/pwgen.rb', line 13 def initialize(length, phonemes = nil) @password = Password.new(length) @phonemes = phonemes || Phonemes.new @mutators = {} #TODO: избавиться от константы @dipthong_length = 2 end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
10 11 12 |
# File 'lib/ppg/pwgen.rb', line 10 def password @password end |
#phonemes=(value) ⇒ Object (writeonly)
Sets the attribute phonemes
11 12 13 |
# File 'lib/ppg/pwgen.rb', line 11 def phonemes=(value) @phonemes = value end |
Instance Method Details
#add_mutator(count, proc) ⇒ Object
22 23 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 51 |
# File 'lib/ppg/pwgen.rb', line 22 def add_mutator(count, proc) @mutated_chars ||= [] @clean_chars_left ||= @password.PASSWORD_LENGTH return false if count == 0 if @clean_chars_left == 0 count = 0 raise MutatorError, "nothing left to mutate, rejecting!" end unless proc.respond_to? :call raise MutatorError, "mutator's not callable, rejecting!" end if count > @clean_chars_left error_string = "can't mutate #{count} characters, decreasing to #{@clean_chars_left}!\n" count = @clean_chars_left raise MutatorError, error_string end @clean_chars_left -= count @mutators[proc] = count true rescue MutatorError STDERR << $!. retry end |
#fill_in_password_pattern! ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ppg/pwgen.rb', line 60 def fill_in_password_pattern! @password.password_pattern.each do |pattern| if pattern.kind_of?(Symbol) elem = @phonemes[pattern][SecureRandom.random_number(@phonemes[pattern].length)] else pattern.kind_of?(Array) # most definitely is dipthong length = @phonemes[pattern[0]][pattern[1]].length elem = @phonemes[pattern[0]][pattern[1]][SecureRandom.random_number(length)] end @password.password << elem end self end |
#generate_password ⇒ Object
53 54 55 56 57 58 |
# File 'lib/ppg/pwgen.rb', line 53 def generate_password generate_password_pattern! .fill_in_password_pattern! .mutate! @password end |
#generate_password_pattern! ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 |
# File 'lib/ppg/pwgen.rb', line 74 def generate_password_pattern! length_left = @password.PASSWORD_LENGTH next_pattern = previous_pattern = nil can_be_next_patterns = @phonemes.keys selector_block = Proc.new { |pattern| pattern if pattern != next_pattern || pattern == :dipthong } while length_left > 0 do loop do next_pattern = generate_random_pattern(can_be_next_patterns) break unless requirements_not_met?(next_pattern, previous_pattern, length_left) end can_be_next_patterns.select! &selector_block can_be_next_patterns << previous_pattern if previous_pattern != nil if next_pattern == :dipthong if @password.password_pattern.empty? #first element auxiliary_pattern = generate_random_pattern([:vowel, :consonant]) else auxiliary_pattern = case previous_pattern when :vowel then :consonant when :consonant then :vowel end end next_pattern = next_pattern, auxiliary_pattern can_be_next_patterns -= [auxiliary_pattern] length_left -= @dipthong_length previous_pattern = auxiliary_pattern else length_left -= 1 previous_pattern = next_pattern end @password.password_pattern << next_pattern end self end |
#mutate! ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ppg/pwgen.rb', line 117 def mutate! @mutators.each_pair do |mutator, count| while count > 0 loop do index = SecureRandom.random_number(@password.PASSWORD_LENGTH) next unless @password.password[index] =~ @phonemes.downcase_regexp @password.password[index] = (mutator.call @password.password[index]) break end count -= 1 end end end |