gitee.com/lonely0422/gometalinter.git@v3.0.1-0.20190307123442-32416ab75314+incompatible/_linters/src/github.com/nbutton23/zxcvbn-go/matching/repeatMatch.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 REPEAT_MATCHER_NAME = "REPEAT"
    11  
    12  func FilterRepeatMatcher(m match.Matcher) bool {
    13  	return m.ID == REPEAT_MATCHER_NAME
    14  }
    15  
    16  func repeatMatch(password string) []match.Match {
    17  	var matches []match.Match
    18  
    19  	//Loop through password. if current == prev currentStreak++ else if currentStreak > 2 {buildMatch; currentStreak = 1} prev = current
    20  	var current, prev string
    21  	currentStreak := 1
    22  	var i int
    23  	var char rune
    24  	for i, char = range password {
    25  		current = string(char)
    26  		if i == 0 {
    27  			prev = current
    28  			continue
    29  		}
    30  
    31  		if strings.ToLower(current) == strings.ToLower(prev) {
    32  			currentStreak++
    33  
    34  		} else if currentStreak > 2 {
    35  			iPos := i - currentStreak
    36  			jPos := i - 1
    37  			matchRepeat := match.Match{
    38  				Pattern:        "repeat",
    39  				I:              iPos,
    40  				J:              jPos,
    41  				Token:          password[iPos : jPos+1],
    42  				DictionaryName: prev}
    43  			matchRepeat.Entropy = entropy.RepeatEntropy(matchRepeat)
    44  			matches = append(matches, matchRepeat)
    45  			currentStreak = 1
    46  		} else {
    47  			currentStreak = 1
    48  		}
    49  
    50  		prev = current
    51  	}
    52  
    53  	if currentStreak > 2 {
    54  		iPos := i - currentStreak + 1
    55  		jPos := i
    56  		matchRepeat := match.Match{
    57  			Pattern:        "repeat",
    58  			I:              iPos,
    59  			J:              jPos,
    60  			Token:          password[iPos : jPos+1],
    61  			DictionaryName: prev}
    62  		matchRepeat.Entropy = entropy.RepeatEntropy(matchRepeat)
    63  		matches = append(matches, matchRepeat)
    64  	}
    65  	return matches
    66  }