Class: Zxcvbn::Matchers::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/zxcvbn/matchers/dictionary.rb

Overview

Given a password and a dictionary, match on any sequential segment of the lowercased password in the dictionary

Instance Method Summary collapse

Constructor Details

#initialize(name, ranked_dictionary) ⇒ Dictionary

Returns a new instance of Dictionary.



7
8
9
10
# File 'lib/zxcvbn/matchers/dictionary.rb', line 7

def initialize(name, ranked_dictionary)
  @name = name
  @ranked_dictionary = ranked_dictionary
end

Instance Method Details

#matches(password) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zxcvbn/matchers/dictionary.rb', line 12

def matches(password)
  results = []
  password_length = password.length
  lowercased_password = password.downcase
  (0..password_length).each do |i|
    (i...password_length).each do |j|
      word = lowercased_password[i..j]
      if @ranked_dictionary.has_key?(word)
        results << Match.new(:matched_word => word,
                             :token => password[i..j],
                             :i => i,
                             :j => j,
                             :rank => @ranked_dictionary[word],
                             :pattern => 'dictionary',
                             :dictionary_name => @name)
      end
    end
  end
  results
end