dictionaryMatch.go 1.2 KB

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