gitee.com/lonely0422/gometalinter.git@v3.0.1-0.20190307123442-32416ab75314+incompatible/_linters/src/github.com/nbutton23/zxcvbn-go/matching/sequenceMatch.go (about)

     1  package matching
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/nbutton23/zxcvbn-go/entropy"
     7  	"github.com/nbutton23/zxcvbn-go/match"
     8  )
     9  
    10  const SEQUENCE_MATCHER_NAME = "SEQ"
    11  
    12  func FilterSequenceMatcher(m match.Matcher) bool {
    13  	return m.ID == SEQUENCE_MATCHER_NAME
    14  }
    15  
    16  func sequenceMatch(password string) []match.Match {
    17  	var matches []match.Match
    18  	for i := 0; i < len(password); {
    19  		j := i + 1
    20  		var seq string
    21  		var seqName string
    22  		seqDirection := 0
    23  		for seqCandidateName, seqCandidate := range SEQUENCES {
    24  			iN := strings.Index(seqCandidate, string(password[i]))
    25  			var jN int
    26  			if j < len(password) {
    27  				jN = strings.Index(seqCandidate, string(password[j]))
    28  			} else {
    29  				jN = -1
    30  			}
    31  
    32  			if iN > -1 && jN > -1 {
    33  				direction := jN - iN
    34  				if direction == 1 || direction == -1 {
    35  					seq = seqCandidate
    36  					seqName = seqCandidateName
    37  					seqDirection = direction
    38  					break
    39  				}
    40  			}
    41  
    42  		}
    43  
    44  		if seq != "" {
    45  			for {
    46  				var prevN, curN int
    47  				if j < len(password) {
    48  					prevChar, curChar := password[j-1], password[j]
    49  					prevN, curN = strings.Index(seq, string(prevChar)), strings.Index(seq, string(curChar))
    50  				}
    51  
    52  				if j == len(password) || curN-prevN != seqDirection {
    53  					if j-i > 2 {
    54  						matchSequence := match.Match{
    55  							Pattern:        "sequence",
    56  							I:              i,
    57  							J:              j - 1,
    58  							Token:          password[i:j],
    59  							DictionaryName: seqName,
    60  						}
    61  
    62  						matchSequence.Entropy = entropy.SequenceEntropy(matchSequence, len(seq), (seqDirection == 1))
    63  						matches = append(matches, matchSequence)
    64  					}
    65  					break
    66  				} else {
    67  					j += 1
    68  				}
    69  
    70  			}
    71  		}
    72  		i = j
    73  	}
    74  	return matches
    75  }