From f460d783b1d301c33d0789b9b5689ab1c188140e Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Sun, 19 Jun 2016 22:33:56 +0900 Subject: [PATCH] gofmt -s --- entropy/entropyCalculator.go | 6 ++-- entropy/entropyCalculator_test.go | 33 ++++++++++--------- matching/dateMatchers.go | 53 +++++++++++++++---------------- matching/dictionaryMatch.go | 2 +- matching/leet.go | 14 ++++---- matching/matching.go | 10 +++--- matching/matching_test.go | 25 ++++++--------- matching/repeatMatch.go | 5 ++- matching/sequenceMatch.go | 11 +++---- matching/spatialMatch.go | 7 ++-- testapp/main.go | 4 +-- zxcvbn.go | 1 - zxcvbn_test.go | 6 ++-- 13 files changed, 81 insertions(+), 96 deletions(-) diff --git a/entropy/entropyCalculator.go b/entropy/entropyCalculator.go index 3046399..028732d 100644 --- a/entropy/entropyCalculator.go +++ b/entropy/entropyCalculator.go @@ -13,9 +13,9 @@ const ( START_UPPER string = `^[A-Z][^A-Z]+$` END_UPPER string = `^[^A-Z]+[A-Z]$'` ALL_UPPER string = `^[A-Z]+$` - NUM_YEARS = float64(119) // years match against 1900 - 2019 - NUM_MONTHS = float64(12) - NUM_DAYS = float64(31) + NUM_YEARS = float64(119) // years match against 1900 - 2019 + NUM_MONTHS = float64(12) + NUM_DAYS = float64(31) ) var ( diff --git a/entropy/entropyCalculator_test.go b/entropy/entropyCalculator_test.go index 07c26bc..594b9be 100644 --- a/entropy/entropyCalculator_test.go +++ b/entropy/entropyCalculator_test.go @@ -1,12 +1,11 @@ package entropy import ( - "github.com/stretchr/testify/assert" "github.com/nbutton23/zxcvbn-go/match" + "github.com/stretchr/testify/assert" "testing" ) - func TestDictionaryEntropyCalculation(t *testing.T) { match := match.Match{ Pattern: "dictionary", @@ -22,31 +21,31 @@ func TestDictionaryEntropyCalculation(t *testing.T) { func TestSpatialEntropyCalculation(t *testing.T) { matchPlain := match.Match{ - Pattern: "spatial", - I: 0, - J: 5, - Token: "asdfgh", - DictionaryName:"qwerty", + Pattern: "spatial", + I: 0, + J: 5, + Token: "asdfgh", + DictionaryName: "qwerty", } entropy := SpatialEntropy(matchPlain, 0, 0) assert.Equal(t, 9.754887502163468, entropy) matchShift := match.Match{ - Pattern: "spatial", - I: 0, - J: 5, - Token: "asdFgh", - DictionaryName:"qwerty", + Pattern: "spatial", + I: 0, + J: 5, + Token: "asdFgh", + DictionaryName: "qwerty", } entropyShift := SpatialEntropy(matchShift, 0, 1) assert.Equal(t, 12.562242424221072, entropyShift) matchTurn := match.Match{ - Pattern: "spatial", - I: 0, - J: 5, - Token: "asdcxz", - DictionaryName:"qwerty", + Pattern: "spatial", + I: 0, + J: 5, + Token: "asdcxz", + DictionaryName: "qwerty", } entropyTurn := SpatialEntropy(matchTurn, 2, 0) assert.Equal(t, 14.080500893768884, entropyTurn) diff --git a/matching/dateMatchers.go b/matching/dateMatchers.go index e3b39e5..dc5c7ac 100644 --- a/matching/dateMatchers.go +++ b/matching/dateMatchers.go @@ -1,15 +1,13 @@ package matching import ( + "github.com/nbutton23/zxcvbn-go/entropy" "github.com/nbutton23/zxcvbn-go/match" - "strings" "regexp" "strconv" - "github.com/nbutton23/zxcvbn-go/entropy" + "strings" ) - - func checkDate(day, month, year int64) (bool, int64, int64, int64) { if (12 <= month && month <= 31) && day <= 12 { day, month = month, day @@ -31,11 +29,11 @@ func dateSepMatcher(password string) []match.Match { var matches []match.Match for _, dateMatch := range dateMatches { match := match.Match{ - I:dateMatch.I, - J:dateMatch.J, - Entropy:entropy.DateEntropy(dateMatch), - DictionaryName:"date_match", - Token:dateMatch.Token, + I: dateMatch.I, + J: dateMatch.J, + Entropy: entropy.DateEntropy(dateMatch), + DictionaryName: "date_match", + Token: dateMatch.Token, } matches = append(matches, match) @@ -55,7 +53,7 @@ func dateSepMatchHelper(password string) []match.DateMatch { day, _ := strconv.ParseInt(splitV[0][4], 10, 16) month, _ := strconv.ParseInt(splitV[0][2], 10, 16) year, _ := strconv.ParseInt(splitV[0][6], 10, 16) - match := match.DateMatch{Day: day, Month: month, Year: year, Separator: splitV[0][5], I: i, J: j, Token:password[i:j]} + match := match.DateMatch{Day: day, Month: month, Year: year, Separator: splitV[0][5], I: i, J: j, Token: password[i:j]} matches = append(matches, match) } @@ -67,7 +65,7 @@ func dateSepMatchHelper(password string) []match.DateMatch { day, _ := strconv.ParseInt(splitV[0][4], 10, 16) month, _ := strconv.ParseInt(splitV[0][6], 10, 16) year, _ := strconv.ParseInt(splitV[0][2], 10, 16) - match := match.DateMatch{Day: day, Month: month, Year: year, Separator: splitV[0][5], I: i, J: j, Token:password[i:j]} + match := match.DateMatch{Day: day, Month: month, Year: year, Separator: splitV[0][5], I: i, J: j, Token: password[i:j]} matches = append(matches, match) } @@ -92,22 +90,23 @@ type DateMatchCandidate struct { } type DateMatchCandidateTwo struct { - Day string + Day string Month string - Year string - I, J int + Year string + I, J int } -func dateWithoutSepMatch(password string) ([]match.Match) { + +func dateWithoutSepMatch(password string) []match.Match { dateMatches := dateWithoutSepMatchHelper(password) var matches []match.Match for _, dateMatch := range dateMatches { match := match.Match{ - I:dateMatch.I, - J:dateMatch.J, - Entropy:entropy.DateEntropy(dateMatch), - DictionaryName:"date_match", - Token:dateMatch.Token, + I: dateMatch.I, + J: dateMatch.J, + Entropy: entropy.DateEntropy(dateMatch), + DictionaryName: "date_match", + Token: dateMatch.Token, } matches = append(matches, match) @@ -115,6 +114,7 @@ func dateWithoutSepMatch(password string) ([]match.Match) { return matches } + //TODO Has issues with 6 digit dates func dateWithoutSepMatchHelper(password string) (matches []match.DateMatch) { matcher := regexp.MustCompile(DATE_WITHOUT_SEP_MATCH) @@ -140,16 +140,15 @@ func dateWithoutSepMatchHelper(password string) (matches []match.DateMatch) { candidatesRoundOne = append(candidatesRoundOne, buildDateMatchCandidate(v[0:lastIndex-3], v[lastIndex-3:], i, j)) } - var candidatesRoundTwo []DateMatchCandidateTwo for _, c := range candidatesRoundOne { if len(c.DayMonth) == 2 { candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:0], c.DayMonth[1:1], c.Year, c.I, c.J)) } else if len(c.DayMonth) == 3 { - candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:2],c.DayMonth[2:2], c.Year, c.I, c.J)) - candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:0],c.DayMonth[1:3], c.Year, c.I, c.J)) + candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:2], c.DayMonth[2:2], c.Year, c.I, c.J)) + candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:0], c.DayMonth[1:3], c.Year, c.I, c.J)) } else if len(c.DayMonth) == 4 { - candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:2],c.DayMonth[2:4], c.Year, c.I, c.J)) + candidatesRoundTwo = append(candidatesRoundTwo, buildDateMatchCandidateTwo(c.DayMonth[0:2], c.DayMonth[2:4], c.Year, c.I, c.J)) } } @@ -159,21 +158,19 @@ func dateWithoutSepMatchHelper(password string) (matches []match.DateMatch) { continue } - intMonth, err := strconv.ParseInt(candidate.Month, 10, 16) if err != nil { continue } - intYear, err := strconv.ParseInt(candidate.Year, 10, 16) if err != nil { continue } - if ok, _, _, _:= checkDate(intDay, intMonth, intYear); ok{ - matches = append(matches, match.DateMatch{Token:password, Pattern:"date", Day:intDay, Month:intMonth, Year:intYear, I:i, J:j }) + if ok, _, _, _ := checkDate(intDay, intMonth, intYear); ok { + matches = append(matches, match.DateMatch{Token: password, Pattern: "date", Day: intDay, Month: intMonth, Year: intYear, I: i, J: j}) } } diff --git a/matching/dictionaryMatch.go b/matching/dictionaryMatch.go index 77164e9..b76921f 100644 --- a/matching/dictionaryMatch.go +++ b/matching/dictionaryMatch.go @@ -1,8 +1,8 @@ package matching import ( - "github.com/nbutton23/zxcvbn-go/match" "github.com/nbutton23/zxcvbn-go/entropy" + "github.com/nbutton23/zxcvbn-go/match" "strings" ) diff --git a/matching/leet.go b/matching/leet.go index 8adf110..29700e8 100644 --- a/matching/leet.go +++ b/matching/leet.go @@ -1,11 +1,11 @@ package matching + import ( - "github.com/nbutton23/zxcvbn-go/match" "github.com/nbutton23/zxcvbn-go/entropy" + "github.com/nbutton23/zxcvbn-go/match" "strings" ) - func l33tMatch(password string) []match.Match { substitutions := relevantL33tSubtable(password) @@ -16,7 +16,7 @@ func l33tMatch(password string) []match.Match { for _, permutation := range permutations { for _, mather := range DICTIONARY_MATCHERS { - matches = append(matches,mather(permutation)...) + matches = append(matches, mather(permutation)...) } } @@ -37,13 +37,13 @@ func getAllPermutationsOfLeetSubstitutions(password string, substitutionsMap map for _, sub := range splice { if string(char) == sub { var permutation string - permutation = password[:index]+value+password[index+1:] + permutation = password[:index] + value + password[index+1:] permutations = append(permutations, permutation) if index < len(permutation) { - tempPermutations := getAllPermutationsOfLeetSubstitutions(permutation[index + 1:], substitutionsMap) + tempPermutations := getAllPermutationsOfLeetSubstitutions(permutation[index+1:], substitutionsMap) for _, temp := range tempPermutations { - permutations = append(permutations, permutation[:index + 1] + temp) + permutations = append(permutations, permutation[:index+1]+temp) } } @@ -65,4 +65,4 @@ func relevantL33tSubtable(password string) map[string][]string { } } return relevantSubs -} \ No newline at end of file +} diff --git a/matching/matching.go b/matching/matching.go index d913f35..024a284 100644 --- a/matching/matching.go +++ b/matching/matching.go @@ -8,10 +8,10 @@ import ( ) var ( - DICTIONARY_MATCHERS []func(password string) []match.Match - MATCHERS []func(password string) []match.Match - ADJACENCY_GRAPHS []adjacency.AdjacencyGraph - L33T_TABLE adjacency.AdjacencyGraph + DICTIONARY_MATCHERS []func(password string) []match.Match + MATCHERS []func(password string) []match.Match + ADJACENCY_GRAPHS []adjacency.AdjacencyGraph + L33T_TABLE adjacency.AdjacencyGraph SEQUENCES map[string]string ) @@ -74,6 +74,4 @@ func loadFrequencyList() { MATCHERS = append(MATCHERS, dateSepMatcher) MATCHERS = append(MATCHERS, dateWithoutSepMatch) - } - diff --git a/matching/matching_test.go b/matching/matching_test.go index 1c5dfab..8a8699b 100644 --- a/matching/matching_test.go +++ b/matching/matching_test.go @@ -1,13 +1,13 @@ package matching import ( - "github.com/stretchr/testify/assert" + "encoding/json" + "fmt" "github.com/nbutton23/zxcvbn-go/match" + "github.com/stretchr/testify/assert" + "log" "strings" "testing" - "fmt" - "encoding/json" - "log" ) //DateSepMatch("1991-09-11jibjab11.9.1991") @@ -123,18 +123,16 @@ func TestDateWithoutSepMatch(t *testing.T) { matches := dateWithoutSepMatch("11091991") assert.Len(t, matches, 1, "Lenght should be 1") - matches = dateWithoutSepMatch("20010911") assert.Len(t, matches, 1, "Lenght should be 1") log.Println(matches) - //matches := dateWithoutSepMatch("110991") //assert.Len(t, matches, 21, "Lenght should be blarg") } //l33t -func TestLeetSubTable(t *testing.T){ +func TestLeetSubTable(t *testing.T) { subs := relevantL33tSubtable("password") assert.Len(t, subs, 0, "password should produce no leet subs") @@ -146,42 +144,39 @@ func TestLeetSubTable(t *testing.T){ assert.Equal(t, subs["i"][0], "1") assert.Equal(t, subs["l"][0], "1") - subs = relevantL33tSubtable("4pple@pple") assert.Len(t, subs, 1, "4pple@pple should produce 1 subs") assert.Len(t, subs["a"], 2) - } -func TestPermutationsOfLeetSubstitutions(t *testing.T){ - password := "p4ssw0rd" //[passw0rd, password, p4ssword] +func TestPermutationsOfLeetSubstitutions(t *testing.T) { + password := "p4ssw0rd" //[passw0rd, password, p4ssword] possibleSubs := relevantL33tSubtable(password) permutations := getAllPermutationsOfLeetSubstitutions(password, possibleSubs) assert.Len(t, permutations, 3, "There should be 3 permutations for "+password) - password = "p4$sw0rd" //[pa$sw0rd, passw0rd, password, pa$sword, p4ssw0rd, p4ssword, p4$sword] + password = "p4$sw0rd" //[pa$sw0rd, passw0rd, password, pa$sword, p4ssw0rd, p4ssword, p4$sword] possibleSubs = relevantL33tSubtable(password) permutations = getAllPermutationsOfLeetSubstitutions(password, possibleSubs) assert.Len(t, permutations, 7, "There should be 7 (? check my math) permutations for "+password) - password = "p4$$w0rd" //[pa$sw0rd, passw0rd, password, pa$sword, p4ssw0rd, p4ssword, p4$sword] + password = "p4$$w0rd" //[pa$sw0rd, passw0rd, password, pa$sword, p4ssw0rd, p4ssword, p4$sword] possibleSubs = relevantL33tSubtable(password) permutations = getAllPermutationsOfLeetSubstitutions(password, possibleSubs) assert.Len(t, permutations, 15, "Check my math 2*2*2*2 - 1 "+password) - password = "1337" possibleSubs = relevantL33tSubtable(password) permutations = getAllPermutationsOfLeetSubstitutions(password, possibleSubs) assert.Len(t, permutations, 35, "check my math 3*2*2*3 -1 ") } -func TestLeet(t *testing.T){ +func TestLeet(t *testing.T) { password := "1337" matches := l33tMatch(password) bytes, _ := json.Marshal(matches) diff --git a/matching/repeatMatch.go b/matching/repeatMatch.go index d3226a4..eed7186 100644 --- a/matching/repeatMatch.go +++ b/matching/repeatMatch.go @@ -1,12 +1,11 @@ package matching import ( - "github.com/nbutton23/zxcvbn-go/match" "github.com/nbutton23/zxcvbn-go/entropy" + "github.com/nbutton23/zxcvbn-go/match" "strings" ) - func repeatMatch(password string) []match.Match { var matches []match.Match @@ -57,4 +56,4 @@ func repeatMatch(password string) []match.Match { matches = append(matches, matchRepeat) } return matches -} \ No newline at end of file +} diff --git a/matching/sequenceMatch.go b/matching/sequenceMatch.go index 64ca209..f75aebf 100644 --- a/matching/sequenceMatch.go +++ b/matching/sequenceMatch.go @@ -1,12 +1,11 @@ package matching import ( - "github.com/nbutton23/zxcvbn-go/match" "github.com/nbutton23/zxcvbn-go/entropy" + "github.com/nbutton23/zxcvbn-go/match" "strings" ) - func sequenceMatch(password string) []match.Match { var matches []match.Match for i := 0; i < len(password); { @@ -39,12 +38,12 @@ func sequenceMatch(password string) []match.Match { for { var prevN, curN int if j < len(password) { - prevChar, curChar := password[j - 1], password[j] + prevChar, curChar := password[j-1], password[j] prevN, curN = strings.Index(seq, string(prevChar)), strings.Index(seq, string(curChar)) } - if j == len(password) || curN - prevN != seqDirection { - if j - i > 2 { + if j == len(password) || curN-prevN != seqDirection { + if j-i > 2 { matchSequence := match.Match{ Pattern: "sequence", I: i, @@ -66,4 +65,4 @@ func sequenceMatch(password string) []match.Match { i = j } return matches -} \ No newline at end of file +} diff --git a/matching/spatialMatch.go b/matching/spatialMatch.go index d804a3e..e2db0f3 100644 --- a/matching/spatialMatch.go +++ b/matching/spatialMatch.go @@ -1,10 +1,10 @@ package matching import ( - "github.com/nbutton23/zxcvbn-go/match" - "github.com/nbutton23/zxcvbn-go/entropy" - "strings" "github.com/nbutton23/zxcvbn-go/adjacency" + "github.com/nbutton23/zxcvbn-go/entropy" + "github.com/nbutton23/zxcvbn-go/match" + "strings" ) func spatialMatch(password string) (matches []match.Match) { @@ -78,4 +78,3 @@ func spatialMatchHelper(password string, graph adjacency.AdjacencyGraph) (matche } return matches } - diff --git a/testapp/main.go b/testapp/main.go index 58b1afc..789ef89 100644 --- a/testapp/main.go +++ b/testapp/main.go @@ -7,8 +7,8 @@ import ( ) func main() { - //password :="Testaaatyhg890l33t" - //fmt.Println(zxcvbn.PasswordStrength(password, nil)) + //password :="Testaaatyhg890l33t" + //fmt.Println(zxcvbn.PasswordStrength(password, nil)) length := 5 //pass := "68f9698fe2540c525fe35b15c6ae1a1788e079962b2ada3d1872c7665c95e148" diff --git a/zxcvbn.go b/zxcvbn.go index 1391b46..510fe96 100644 --- a/zxcvbn.go +++ b/zxcvbn.go @@ -7,7 +7,6 @@ import ( "time" ) - func PasswordStrength(password string, userInputs []string) scoring.MinEntropyMatch { start := time.Now() matches := matching.Omnimatch(password, userInputs) diff --git a/zxcvbn_test.go b/zxcvbn_test.go index 6e04ab5..a25bd92 100644 --- a/zxcvbn_test.go +++ b/zxcvbn_test.go @@ -28,7 +28,7 @@ var numTestRan int func TestPasswordStrength(t *testing.T) { -// Expected calculated by running zxcvbn-python + // Expected calculated by running zxcvbn-python runTest(t, "zxcvbn", float64(6.845490050944376)) runTest(t, "Tr0ub4dour&3", float64(17.296)) runTest(t, "qwER43@!", float64(26.44)) @@ -36,8 +36,8 @@ func TestPasswordStrength(t *testing.T) { runTest(t, "coRrecth0rseba++ery9.23.2007staple$", float64(66.018)) runTest(t, "D0g..................", float64(20.678)) runTest(t, "abcdefghijk987654321", float64(11.951)) - runTest(t, "neverforget", float64(2)) // I think this is wrong. . . - runTest(t, "13/3/1997", float64(2)) // I think this is wrong. . . + runTest(t, "neverforget", float64(2)) // I think this is wrong. . . + runTest(t, "13/3/1997", float64(2)) // I think this is wrong. . . runTest(t, "neverforget13/3/1997", float64(32.628)) runTest(t, "1qaz2wsx3edc", float64(19.314)) runTest(t, "temppass22", float64(22.179))