repeatMatch.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package matching
  2. import (
  3. "strings"
  4. "github.com/nbutton23/zxcvbn-go/entropy"
  5. "github.com/nbutton23/zxcvbn-go/match"
  6. )
  7. const REPEAT_MATCHER_NAME = "REPEAT"
  8. func FilterRepeatMatcher(m match.Matcher) bool {
  9. return m.ID == REPEAT_MATCHER_NAME
  10. }
  11. func repeatMatch(password string) []match.Match {
  12. var matches []match.Match
  13. //Loop through password. if current == prev currentStreak++ else if currentStreak > 2 {buildMatch; currentStreak = 1} prev = current
  14. var current, prev string
  15. currentStreak := 1
  16. var i int
  17. var char rune
  18. for i, char = range password {
  19. current = string(char)
  20. if i == 0 {
  21. prev = current
  22. continue
  23. }
  24. if strings.ToLower(current) == strings.ToLower(prev) {
  25. currentStreak++
  26. } else if currentStreak > 2 {
  27. iPos := i - currentStreak
  28. jPos := i - 1
  29. matchRepeat := match.Match{
  30. Pattern: "repeat",
  31. I: iPos,
  32. J: jPos,
  33. Token: password[iPos : jPos+1],
  34. DictionaryName: prev}
  35. matchRepeat.Entropy = entropy.RepeatEntropy(matchRepeat)
  36. matches = append(matches, matchRepeat)
  37. currentStreak = 1
  38. } else {
  39. currentStreak = 1
  40. }
  41. prev = current
  42. }
  43. if currentStreak > 2 {
  44. iPos := i - currentStreak + 1
  45. jPos := i
  46. matchRepeat := match.Match{
  47. Pattern: "repeat",
  48. I: iPos,
  49. J: jPos,
  50. Token: password[iPos : jPos+1],
  51. DictionaryName: prev}
  52. matchRepeat.Entropy = entropy.RepeatEntropy(matchRepeat)
  53. matches = append(matches, matchRepeat)
  54. }
  55. return matches
  56. }