Module: StrongPassword::PasswordVariants

Defined in:
lib/strong_password/password_variants.rb

Constant Summary collapse

LEET_SPEAK_REGEXP =
/[\@\!\$1234567890]/
LEET_SPEAK =
{
  "@" => "a",
  "!" => "i",
  "$" => "s",
  "1" => "i",
  "2" => "z",
  "3" => "e",
  "4" => "a",
  "5" => "s",
  "6" => "g",
  "7" => "t",
  "8" => "b",
  "9" => "g",
  "0" => "o"
}
LEET_SPEAK_ALT =
LEET_SPEAK.dup.merge!("1" => "l")
BOTTOM_ROW_REGEXP =
/[zxcvbnm,\.\/\<\>\?]/
KEYBOARDMAP_DOWNRIGHT =
{
  "a" => "z",
  "q" => "a",
  "1" => "q",
  "s" => "x",
  "w" => "s",
  "2" => "w",
  "d" => "c",
  "e" => "d",
  "3" => "e",
  "f" => "v",
  "r" => "f",
  "4" => "r",
  "g" => "b",
  "t" => "g",
  "5" => "t",
  "h" => "n",
  "y" => "h",
  "6" => "y",
  "j" => "m",
  "u" => "j",
  "7" => "u",
  "i" => "k",
  "8" => "i",
  "o" => "l",
  "9" => "o",
  "0" => "p"
}
KEYBOARDMAP_DOWNLEFT =
{
  "2" => "q",
  "w" => "a",
  "3" => "w",
  "s" => "z",
  "e" => "s",
  "4" => "e",
  "d" => "x",
  "r" => "d",
  "5" => "r",
  "f" => "c",
  "t" => "f",
  "6" => "t",
  "g" => "v",
  "y" => "g",
  "7" => "y",
  "h" => "b",
  "u" => "h",
  "8" => "u",
  "j" => "n",
  "i" => "j",
  "9" => "i",
  "k" => "m",
  "o" => "k",
  "0" => "o",
  "p" => "l",
  "-" => "p"
}

Class Method Summary collapse

Class Method Details

.all_variants(password) ⇒ Object

Returns all variants of a given password including the password itself



84
85
86
87
88
89
# File 'lib/strong_password/password_variants.rb', line 84

def self.all_variants(password)
  passwords = [password.downcase]
  passwords += keyboard_shift_variants(password)
  passwords += leet_speak_variants(password)
  passwords.uniq
end

.keyboard_shift_variants(password) ⇒ Object

Returns all keyboard shifted variants of a given password



92
93
94
95
96
97
# File 'lib/strong_password/password_variants.rb', line 92

def self.keyboard_shift_variants(password)
  password = password.downcase

  return [] if password.match(BOTTOM_ROW_REGEXP)
  variants(password, KEYBOARDMAP_DOWNRIGHT, KEYBOARDMAP_DOWNLEFT)
end

.leet_speak_variants(password) ⇒ Object

Returns all leet speak variants of a given password



100
101
102
103
104
105
# File 'lib/strong_password/password_variants.rb', line 100

def self.leet_speak_variants(password)
  password = password.downcase

  return [] if !password.match(LEET_SPEAK_REGEXP)
  variants(password, LEET_SPEAK, LEET_SPEAK_ALT)
end