Class: Zxcvbn::Matchers::Sequences Private

Inherits:
Object
  • Object
show all
Defined in:
lib/zxcvbn/matchers/sequences.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.

Matches monotonically incrementing or decrementing character sequences, such as “abcde”, “54321”, or “ZYXW”.

Constant Summary collapse

MAX_DELTA =

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.

Maximum absolute step between adjacent characters for a valid sequence.

5
ALL_LOWER =

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.

Matches tokens that are all lowercase letters.

/^[a-z]+$/
ALL_UPPER =

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.

Matches tokens that are all uppercase letters.

/^[A-Z]+$/
ALL_DIGITS =

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.

Matches tokens that are all digits.

/^\d+$/

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.

Returns matches with pattern “sequence”.

Parameters:

  • password (String)

Returns:

  • (Array<MatchBuilder>)

    matches with pattern “sequence”



22
23
24
25
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
61
62
63
# File 'lib/zxcvbn/matchers/sequences.rb', line 22

def matches(password)
  return [] if password.length < 2

  result = []
  start = 0
  last_delta = nil

  emit = lambda do |seq_end, delta|
    return if delta.nil?

    abs_delta = delta.abs
    return unless abs_delta.positive? && abs_delta <= MAX_DELTA

    len = seq_end - start + 1
    return unless len > 2 || abs_delta == 1

    token = password[start, len]
    seq_name, seq_space = classify(token)
    result << MatchBuilder.new(
      pattern: 'sequence',
      i: start,
      j: seq_end,
      token:,
      sequence_name: seq_name,
      sequence_space: seq_space,
      ascending: delta.positive?
    )
  end

  (1...password.length).each do |i|
    delta = password[i].ord - password[i - 1].ord
    last_delta = delta if last_delta.nil?
    next if delta == last_delta

    emit.call(i - 1, last_delta)
    start = i - 1
    last_delta = delta
  end
  emit.call(password.length - 1, last_delta)

  result
end