Class: Zxcvbn::Matchers::Sequences

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

Constant Summary collapse

SEQUENCES =
{
  'lower' => 'abcdefghijklmnopqrstuvwxyz',
  'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  'digits' => '01234567890'
}

Instance Method Summary collapse

Instance Method Details

#applicable_sequence(password, i) ⇒ Object

find the first matching sequence, and return with direction, if characters are one apart in the sequence



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zxcvbn/matchers/sequences.rb', line 24

def applicable_sequence(password, i)
  SEQUENCES.each do |name, sequence|
    index1 = sequence.index(password[i])
    index2 = sequence.index(password[i+1])
    if index1 and index2
      seq_direction = index2 - index1
      if [-1, 1].include?(seq_direction)
        return [name, sequence, seq_direction]
      else
        return nil
      end
    end
  end
end

#matches(password) ⇒ Object



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
64
# File 'lib/zxcvbn/matchers/sequences.rb', line 39

def matches(password)
  result = []
  i = 0
  while i < password.length - 1
    seq_name, seq, seq_direction = applicable_sequence(password, i)

    if seq
      length = seq_match_length(password, i, seq_direction, seq)
      if length > 2
        result << Match.new(
          :pattern => 'sequence',
          :i => i,
          :j => i + length - 1,
          :token => password[i, length],
          :sequence_name => seq_name,
          :sequence_space => seq.length,
          :ascending => seq_direction == 1
        )
      end
      i += length - 1
    else
      i += 1
    end
  end
  result
end

#seq_match_length(password, from, direction, seq) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/zxcvbn/matchers/sequences.rb', line 12

def seq_match_length(password, from, direction, seq)
  index_from = seq.index(password[from])
  j = 1
  while from + j < password.length &&
        password[from + j] == seq[index_from + direction * j]
    j+= 1
  end
  j
end