Class: Zxcvbn::Matchers::Repeat Private

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

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.

Finds repeated substrings in a password (e.g. “abcabc”, “aaaa”).

Uses a greedy/lazy regex disambiguation strategy from zxcvbn.js v4: prefer the greedier match unless the lazy match is longer, then use LAZY_ANCHORED to extract the minimal repeating unit (base_token).

Constant Summary collapse

GREEDY =

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

Greedily matches the longest repeated substring.

/(.+)\1+/
LAZY =

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

Lazily matches the shortest repeated substring.

/(.+?)\1+/
LAZY_ANCHORED =

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

Anchored lazy pattern used to extract the minimal base token.

/^(.+?)\1+$/

Instance Method Summary collapse

Instance Method Details

#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.

Find all repeated-substring matches in the password.

Parameters:

  • password (String)

    the password to search

Returns:

  • (Array<MatchBuilder>)

    matches with pattern ‘repeat’, each containing base_token (the repeated unit) and repeat_count



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zxcvbn/matchers/repeat.rb', line 26

def matches(password)
  result = []
  last_index = 0

  while last_index < password.length
    greedy_match = GREEDY.match(password, last_index)
    lazy_match   = LAZY.match(password, last_index)
    break unless greedy_match

    if greedy_match[0].length > lazy_match[0].length
      rx_match   = greedy_match
      base_token = LAZY_ANCHORED.match(rx_match[0])[1]
    else
      rx_match   = lazy_match
      base_token = rx_match[1]
    end

    i     = rx_match.begin(0)
    j     = rx_match.end(0) - 1
    token = rx_match[0]

    result << MatchBuilder.new(
      pattern: 'repeat',
      i:,
      j:,
      token:,
      base_token:,
      repeat_count: token.length / base_token.length
    )

    last_index = j + 1
  end

  result
end