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

     1  package matching
     2  
     3  import (
     4  	"github.com/nbutton23/zxcvbn-go/entropy"
     5  	"github.com/nbutton23/zxcvbn-go/match"
     6  	"strings"
     7  )
     8  
     9  func buildDictMatcher(dictName string, rankedDict map[string]int) func(password string) []match.Match {
    10  	return func(password string) []match.Match {
    11  		matches := dictionaryMatch(password, dictName, rankedDict)
    12  		for _, v := range matches {
    13  			v.DictionaryName = dictName
    14  		}
    15  		return matches
    16  	}
    17  
    18  }
    19  
    20  func dictionaryMatch(password string, dictionaryName string, rankedDict map[string]int) []match.Match {
    21  	length := len(password)
    22  	var results []match.Match
    23  	pwLower := strings.ToLower(password)
    24  
    25  	for i := 0; i < length; i++ {
    26  		for j := i; j < length; j++ {
    27  			word := pwLower[i : j+1]
    28  			if val, ok := rankedDict[word]; ok {
    29  				matchDic := match.Match{Pattern: "dictionary",
    30  					DictionaryName: dictionaryName,
    31  					I:              i,
    32  					J:              j,
    33  					Token:          password[i : j+1],
    34  				}
    35  				matchDic.Entropy = entropy.DictionaryEntropy(matchDic, float64(val))
    36  
    37  				results = append(results, matchDic)
    38  			}
    39  		}
    40  	}
    41  
    42  	return results
    43  }
    44  
    45  func buildRankedDict(unrankedList []string) map[string]int {
    46  
    47  	result := make(map[string]int)
    48  
    49  	for i, v := range unrankedList {
    50  		result[strings.ToLower(v)] = i + 1
    51  	}
    52  
    53  	return result
    54  }