File: leet.go

package info (click to toggle)
golang-github-nbutton23-zxcvbn-go 0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,448 kB
  • sloc: makefile: 6
file content (75 lines) | stat: -rw-r--r-- 1,856 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package matching

import (
	"strings"

	"github.com/nbutton23/zxcvbn-go/entropy"
	"github.com/nbutton23/zxcvbn-go/match"
)

const L33T_MATCHER_NAME = "l33t"

func FilterL33tMatcher(m match.Matcher) bool {
	return m.ID == L33T_MATCHER_NAME
}

func l33tMatch(password string) []match.Match {

	substitutions := relevantL33tSubtable(password)

	permutations := getAllPermutationsOfLeetSubstitutions(password, substitutions)

	var matches []match.Match

	for _, permutation := range permutations {
		for _, mather := range DICTIONARY_MATCHERS {
			matches = append(matches, mather.MatchingFunc(permutation)...)
		}
	}

	for _, match := range matches {
		match.Entropy += entropy.ExtraLeetEntropy(match, password)
		match.DictionaryName = match.DictionaryName + "_3117"
	}

	return matches
}

func getAllPermutationsOfLeetSubstitutions(password string, substitutionsMap map[string][]string) []string {

	var permutations []string

	for index, char := range password {
		for value, splice := range substitutionsMap {
			for _, sub := range splice {
				if string(char) == sub {
					var permutation string
					permutation = password[:index] + value + password[index+1:]

					permutations = append(permutations, permutation)
					if index < len(permutation) {
						tempPermutations := getAllPermutationsOfLeetSubstitutions(permutation[index+1:], substitutionsMap)
						for _, temp := range tempPermutations {
							permutations = append(permutations, permutation[:index+1]+temp)
						}

					}
				}
			}
		}
	}

	return permutations
}

func relevantL33tSubtable(password string) map[string][]string {
	relevantSubs := make(map[string][]string)
	for key, values := range L33T_TABLE.Graph {
		for _, value := range values {
			if strings.Contains(password, value) {
				relevantSubs[key] = append(relevantSubs[key], value)
			}
		}
	}
	return relevantSubs
}