Class: Zxcvbn::Matchers::Dictionary Private

Inherits:
Object
  • Object
show all
Includes:
CaseHelpers
Defined in:
lib/zxcvbn/matchers/dictionary.rb,
sig/zxcvbn/matchers/dictionary.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Matches any sequential segment of the lowercased password that appears in a ranked dictionary.

Instance Method Summary collapse

Methods included from CaseHelpers

downcase_preserving_length, #self?.downcase_preserving_length

Constructor Details

#initialize(name, ranked_dictionary, trie = nil) ⇒ Dictionary

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Dictionary.

Parameters:

  • name (String)

    dictionary identifier used in match results

  • ranked_dictionary (Hash{String => Integer})

    lowercased word → rank

  • trie (Trie, nil) (defaults to: nil)

    optional prefix trie for faster lookups



16
17
18
19
20
# File 'lib/zxcvbn/matchers/dictionary.rb', line 16

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

Instance Method Details

#build_match(matched_word, token, start_pos, end_pos, rank) ⇒ MatchBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • matched_word (String)
  • token (String)
  • start_pos (Integer)
  • end_pos (Integer)
  • rank (Integer)

Returns:



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zxcvbn/matchers/dictionary.rb', line 67

def build_match(matched_word, token, start_pos, end_pos, rank)
  MatchBuilder.new(
    matched_word:,
    token:,
    i: start_pos,
    j: end_pos,
    rank:,
    pattern: 'dictionary',
    dictionary_name: @name
  )
end

#hash_matches(password, lowercased_password) ⇒ Array[MatchBuilder]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • password (String)
  • lowercased_password (String)

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zxcvbn/matchers/dictionary.rb', line 50

def hash_matches(password, lowercased_password)
  results = []
  password_length = password.length

  (0..password_length).each do |i|
    (i...password_length).each do |j|
      length = j - i + 1
      word = lowercased_password.slice(i, length)
      next unless @ranked_dictionary.key?(word)

      results << build_match(word, password.slice(i, length), i, j, @ranked_dictionary[word])
    end
  end

  results
end

#matches(password) ⇒ Array<MatchBuilder>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns all dictionary matches found in password.

Parameters:

  • password (String)

Returns:

  • (Array<MatchBuilder>)

    matches with pattern "dictionary"



26
27
28
29
30
31
32
33
34
# File 'lib/zxcvbn/matchers/dictionary.rb', line 26

def matches(password)
  lowercased_password = downcase_preserving_length(password)

  if @trie
    trie_matches(password, lowercased_password)
  else
    hash_matches(password, lowercased_password)
  end
end

#trie_matches(password, lowercased_password) ⇒ Array[MatchBuilder]

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • password (String)
  • lowercased_password (String)

Returns:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zxcvbn/matchers/dictionary.rb', line 38

def trie_matches(password, lowercased_password)
  results = []

  (0...password.length).each do |i|
    @trie.search_prefixes(lowercased_password, i).each do |word, rank, start, ending|
      results << build_match(word, password.slice(start, ending - start + 1), start, ending, rank)
    end
  end

  results
end