Class: KeePass::Password::Generator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(pattern, options = {}) ⇒ PasswordGenerator

Instantiates a new PasswordGenerator object.

Parameters:

  • pattern (String)

    the pattern

  • options (Hash) (defaults to: {})

    the options

Options Hash (options):

  • :permute (Boolean) — default: true

    whether or not to randomly permute generated passwords

  • :remove_lookalikes (Boolean) — default: false

    whether or not to remove lookalike characters

  • :charset_mapping (Hash) — default: CharSet::DEFAULT_MAPPING

    the KeePass character set ID mapping

Raises:



47
48
49
50
51
# File 'lib/keepass/password/generator.rb', line 47

def initialize(pattern, options = {})
  @permute = options.has_key?(:permute) ? options[:permute] : true
  @pattern = pattern
  @char_sets = pattern_to_char_sets(pattern, options)
end

Instance Attribute Details

#char_setsArray<CharSet> (readonly)

Returns the character sets from the pattern.

Returns:

  • (Array<CharSet>)

    the character sets from the pattern



33
34
35
# File 'lib/keepass/password/generator.rb', line 33

def char_sets
  @char_sets
end

#patternString (readonly)

Returns the pattern.

Returns:

  • (String)

    the pattern



30
31
32
# File 'lib/keepass/password/generator.rb', line 30

def pattern
  @pattern
end

#permuteBoolean

Returns whether or not to permute the password.

Returns:

  • (Boolean)

    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

#generateString

Returns a new password.

Returns:

  • (String)

    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