Class: XKPassword::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/xkpassword/generator.rb

Overview

The Generator class which finds words based on the requirement and using the provided options to build a new random passowrd. Presets provide convenient defaults for common password styles, and callers can override any preset-derived value by passing explicit generation options.

Constant Summary collapse

DEFAULT_PRESET =

The preset used when ‘:preset` is omitted.

:xkcd
PRESETS =

Built-in password presets.

  • ‘:xkcd` generates 4 words between 4 and 8 letters separated by `-`, preserving the gem’s original default behavior.

  • ‘:web32` generates 4 shorter words between 4 and 5 letters separated by `-`.

  • ‘:wifi` generates 6 words between 4 and 8 letters separated by `-`.

  • ‘:security` generates 6 lowercase words between 4 and 8 letters separated by spaces.

  • ‘:apple_id` generates 3 words between 4 and 7 letters separated by `-`.

{
  xkcd: {
    case_transform: nil,
    max_length: 8,
    min_length: 4,
    separator: '-',
    words: 4,
  },
  web32: {
    case_transform: nil,
    max_length: 5,
    min_length: 4,
    separator: '-',
    words: 4,
  },
  wifi: {
    case_transform: nil,
    max_length: 8,
    min_length: 4,
    separator: '-',
    words: 6,
  },
  security: {
    case_transform: :downcase,
    max_length: 8,
    min_length: 4,
    separator: ' ',
    words: 6,
  },
  apple_id: {
    case_transform: nil,
    max_length: 7,
    min_length: 4,
    separator: '-',
    words: 3,
  },
}
VALID_CASE_TRANSFORMS =
[:upcase, :downcase, :capitalize]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



62
63
64
# File 'lib/xkpassword/generator.rb', line 62

def initialize
  @words = XKPassword::Words.new
end

Instance Attribute Details

#wordsXKPassword::Words (readonly)

A word database that gen provide you words for the length required

Returns:



9
10
11
# File 'lib/xkpassword/generator.rb', line 9

def words
  @words
end

Instance Method Details

#generate(options = nil) ⇒ String

Generates a password absed on the configuration provided. A preset supplies a base configuration and any explicit options passed in ‘options` override those preset defaults.

Examples:

Using the default xkcd preset

generator = XKPassword::Generator.new
generator.generate

Using the security preset

generator = XKPassword::Generator.new
generator.generate(preset: :security)

Populating the method with a preset and explicit overrides

options = {
  preset: :security,
  separator: ' ',
  words: 6,
  min_length: 4,
  max_length: 8,
  case_transform: :downcase
}

generator = XKPassword::Generator.new
generator.generate(options)

Built-in preset defaults

:xkcd     # 4 words, 4..8 letters, "-" separator, random per-word uppercasing
:web32    # 4 words, 4..5 letters, "-" separator, random per-word uppercasing
:wifi     # 6 words, 4..8 letters, "-" separator, random per-word uppercasing
:security # 6 words, 4..8 letters, " " separator, lowercase
:apple_id # 3 words, 4..7 letters, "-" separator, random per-word uppercasing

Parameters:

  • options (Hash) (defaults to: nil)

    The options to populate a generator

Options Hash (options):

  • :preset (String, Symbol)

    The preset to use. Supports ‘:xkcd`, `:web32`, `:wifi`, `:security`, and `:apple_id`. String values like `“apple_id”` and `“apple-id”` are normalized to `:apple_id`. Defaults to `:xkcd`.

  • :words (Integer)

    The number of words to include in the generated password

  • :separator (String)

    The separator symbol to use joining words used in password

  • :min_length (Integer)

    The minimum length of a word to be used in the process

  • :max_length (Integer)

    The maximum length of a word to be used in the process

  • :case_transform (String, Symbol)

    The transform to apply to every generated word

Returns:

  • (String)

    The generated password



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xkpassword/generator.rb', line 110

def generate(options = nil)
  options = (options || {}).dup
  preset = normalize_preset(options.delete(:preset))
  options = PRESETS.fetch(preset).merge(options)
  case_transform = normalize_case_transform(options[:case_transform])
  length_vals = (options[:min_length]..options[:max_length]).to_a

  data = options[:words].times.map do
    word = words.random(length_vals.sample)
    transform_word(word, case_transform)
  end
  
  data.join(options[:separator])
end