zxcvbn-go/matching/sequenceMatch.go

77 lines
1.7 KiB
Go
Raw Normal View History

2016-03-08 09:57:50 -07:00
package matching
import (
2017-11-02 14:14:31 +01:00
"strings"
2016-03-08 09:57:50 -07:00
"github.com/nbutton23/zxcvbn-go/entropy"
2016-06-19 22:33:56 +09:00
"github.com/nbutton23/zxcvbn-go/match"
2016-03-08 09:57:50 -07:00
)
const sequenceMatcherName = "SEQ"
2017-11-02 14:14:31 +01:00
//FilterSequenceMatcher can be pass to zxcvbn-go.PasswordStrength to skip that matcher
2017-11-02 14:14:31 +01:00
func FilterSequenceMatcher(m match.Matcher) bool {
return m.ID == sequenceMatcherName
2017-11-02 14:14:31 +01:00
}
2016-03-08 09:57:50 -07:00
func sequenceMatch(password string) []match.Match {
var matches []match.Match
for i := 0; i < len(password); {
j := i + 1
var seq string
var seqName string
seqDirection := 0
for seqCandidateName, seqCandidate := range sequences {
2016-03-08 09:57:50 -07:00
iN := strings.Index(seqCandidate, string(password[i]))
var jN int
if j < len(password) {
jN = strings.Index(seqCandidate, string(password[j]))
} else {
jN = -1
}
if iN > -1 && jN > -1 {
direction := jN - iN
if direction == 1 || direction == -1 {
seq = seqCandidate
seqName = seqCandidateName
seqDirection = direction
break
}
}
}
if seq != "" {
for {
var prevN, curN int
if j < len(password) {
2016-06-19 22:33:56 +09:00
prevChar, curChar := password[j-1], password[j]
2016-03-08 09:57:50 -07:00
prevN, curN = strings.Index(seq, string(prevChar)), strings.Index(seq, string(curChar))
}
2016-06-19 22:33:56 +09:00
if j == len(password) || curN-prevN != seqDirection {
if j-i > 2 {
2016-03-08 09:57:50 -07:00
matchSequence := match.Match{
Pattern: "sequence",
I: i,
J: j - 1,
Token: password[i:j],
DictionaryName: seqName,
}
matchSequence.Entropy = entropy.SequenceEntropy(matchSequence, len(seq), (seqDirection == 1))
matches = append(matches, matchSequence)
}
break
} else {
j++
2016-03-08 09:57:50 -07:00
}
}
}
i = j
}
return matches
2016-06-19 22:33:56 +09:00
}