Class: KeePass::Password::Generator
- Inherits:
-
Object
- Object
- KeePass::Password::Generator
- Defined in:
- lib/keepass/password/generator.rb
Overview
Generate passwords using KeePass password generator patterns.
Constant Summary collapse
- CHARSET_IDS =
Available character sets
CharSet::DEFAULT_MAPPING.keys.join
- LITERALS_RE =
ASCII printables regular expression
/[\x20-\x7e]/
- CHAR_TOKEN_RE =
Regexp.new("([#{CHARSET_IDS}])|\\\\(#{LITERALS_RE.source})")
- GROUP_TOKEN_RE =
Regexp.new("(#{CHAR_TOKEN_RE.source}|" + "\\[((#{CHAR_TOKEN_RE.source})*?)\\])" + "(\\{(\\d+)\\})?")
- VALIDATOR_RE =
Regexp.new("\\A(#{GROUP_TOKEN_RE.source})+\\Z")
- LOOKALIKE =
"O0l1I|"
- LOOKALIKE_CHARSET =
CharSet.new.add_from_strings LOOKALIKE
Instance Attribute Summary collapse
-
#char_sets ⇒ Array<CharSet>
readonly
The character sets from the pattern.
-
#pattern ⇒ String
readonly
The pattern.
-
#permute ⇒ Boolean
Whether or not to permute the password.
Instance Method Summary collapse
-
#generate ⇒ String
Returns a new password.
-
#initialize(pattern, options = {}) ⇒ PasswordGenerator
constructor
Instantiates a new PasswordGenerator object.
Constructor Details
#initialize(pattern, options = {}) ⇒ PasswordGenerator
Instantiates a new PasswordGenerator object.
47 48 49 50 51 |
# File 'lib/keepass/password/generator.rb', line 47 def initialize(pattern, = {}) @permute = .has_key?(:permute) ? [:permute] : true @pattern = pattern @char_sets = pattern_to_char_sets(pattern, ) end |
Instance Attribute Details
#char_sets ⇒ Array<CharSet> (readonly)
Returns the character sets from the pattern.
33 34 35 |
# File 'lib/keepass/password/generator.rb', line 33 def char_sets @char_sets end |
#pattern ⇒ String (readonly)
Returns the pattern.
30 31 32 |
# File 'lib/keepass/password/generator.rb', line 30 def pattern @pattern end |
#permute ⇒ Boolean
Returns whether or not to permute the password.
36 37 38 |
# File 'lib/keepass/password/generator.rb', line 36 def permute @permute end |
Instance Method Details
#generate ⇒ String
Returns a new password.
56 57 58 59 60 |
# File 'lib/keepass/password/generator.rb', line 56 def generate result = char_sets.map { |c| Random.sample_array(c.to_a) } result = Random.shuffle_array(result) if permute result.join end |