2016-03-08 16:57:50 +00:00
|
|
|
package matching
|
|
|
|
|
|
|
|
import (
|
2018-03-28 12:42:36 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
"github.com/nbutton23/zxcvbn-go/entropy"
|
2016-06-19 13:33:56 +00:00
|
|
|
"github.com/nbutton23/zxcvbn-go/match"
|
2016-03-08 16:57:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func buildDictMatcher(dictName string, rankedDict map[string]int) func(password string) []match.Match {
|
|
|
|
return func(password string) []match.Match {
|
|
|
|
matches := dictionaryMatch(password, dictName, rankedDict)
|
|
|
|
for _, v := range matches {
|
|
|
|
v.DictionaryName = dictName
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func dictionaryMatch(password string, dictionaryName string, rankedDict map[string]int) []match.Match {
|
|
|
|
var results []match.Match
|
|
|
|
pwLower := strings.ToLower(password)
|
|
|
|
|
2018-03-28 12:42:36 +00:00
|
|
|
pwLowerRunes := []rune(pwLower)
|
|
|
|
length := len(pwLowerRunes)
|
|
|
|
|
2016-03-08 16:57:50 +00:00
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
for j := i; j < length; j++ {
|
2018-03-28 12:42:36 +00:00
|
|
|
word := pwLowerRunes[i : j+1]
|
|
|
|
if val, ok := rankedDict[string(word)]; ok {
|
2016-03-08 16:57:50 +00:00
|
|
|
matchDic := match.Match{Pattern: "dictionary",
|
|
|
|
DictionaryName: dictionaryName,
|
|
|
|
I: i,
|
|
|
|
J: j,
|
2018-03-28 12:42:36 +00:00
|
|
|
Token: string([]rune(password)[i : j+1]),
|
2016-03-08 16:57:50 +00:00
|
|
|
}
|
|
|
|
matchDic.Entropy = entropy.DictionaryEntropy(matchDic, float64(val))
|
|
|
|
|
|
|
|
results = append(results, matchDic)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildRankedDict(unrankedList []string) map[string]int {
|
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
|
|
|
|
for i, v := range unrankedList {
|
|
|
|
result[strings.ToLower(v)] = i + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|