leet.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package matching
  2. import (
  3. "strings"
  4. "github.com/nbutton23/zxcvbn-go/entropy"
  5. "github.com/nbutton23/zxcvbn-go/match"
  6. )
  7. const L33T_MATCHER_NAME = "l33t"
  8. func FilterL33tMatcher(m match.Matcher) bool {
  9. return m.ID == L33T_MATCHER_NAME
  10. }
  11. func l33tMatch(password string) []match.Match {
  12. substitutions := relevantL33tSubtable(password)
  13. permutations := getAllPermutationsOfLeetSubstitutions(password, substitutions)
  14. var matches []match.Match
  15. for _, permutation := range permutations {
  16. for _, mather := range DICTIONARY_MATCHERS {
  17. matches = append(matches, mather.MatchingFunc(permutation)...)
  18. }
  19. }
  20. for _, match := range matches {
  21. match.Entropy += entropy.ExtraLeetEntropy(match, password)
  22. match.DictionaryName = match.DictionaryName + "_3117"
  23. }
  24. return matches
  25. }
  26. func getAllPermutationsOfLeetSubstitutions(password string, substitutionsMap map[string][]string) []string {
  27. var permutations []string
  28. for index, char := range password {
  29. for value, splice := range substitutionsMap {
  30. for _, sub := range splice {
  31. if string(char) == sub {
  32. var permutation string
  33. permutation = password[:index] + value + password[index+1:]
  34. permutations = append(permutations, permutation)
  35. if index < len(permutation) {
  36. tempPermutations := getAllPermutationsOfLeetSubstitutions(permutation[index+1:], substitutionsMap)
  37. for _, temp := range tempPermutations {
  38. permutations = append(permutations, permutation[:index+1]+temp)
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. return permutations
  46. }
  47. func relevantL33tSubtable(password string) map[string][]string {
  48. relevantSubs := make(map[string][]string)
  49. for key, values := range L33T_TABLE.Graph {
  50. for _, value := range values {
  51. if strings.Contains(password, value) {
  52. relevantSubs[key] = append(relevantSubs[key], value)
  53. }
  54. }
  55. }
  56. return relevantSubs
  57. }