module Zxcvbn::Passgen

Constants

VERSION

Public Class Methods

generate(length = 11) click to toggle source
# File lib/zxcvbn/passgen.rb, line 8
def self.generate(length = 11)
  raise "Password length must be greater or equal 11." if length < 11
  pass = make(length)
  # score 4 to score 3
  while Zxcvbn.test(pass).score < 3
    generate(length)
  end
  pass
end
make(length) click to toggle source
# File lib/zxcvbn/passgen.rb, line 18
def self.make(length)
  down    = ('a'..'z').to_a
  up      = ('A'..'Z').to_a
  digits  = ('0'..'9').to_a
  symbols = %i[~ ! @ # $ % ^ & * ( ) + { } ? < > = ].freeze
  all     = down + up + digits + symbols
  [down.sample, up.sample, digits.sample, symbols.sample]
    .concat((length - 4).times.map { all.sample })
    .shuffle
    .join
end