Merge pull request #17 from shawnps/master

gofmt -s
This commit is contained in:
Nathan Button 2016-06-26 18:44:24 -06:00 committed by GitHub
commit a22cb81b2e
13 changed files with 81 additions and 96 deletions

View File

@ -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",
@ -26,7 +25,7 @@ func TestSpatialEntropyCalculation(t *testing.T) {
I: 0,
J: 5,
Token: "asdfgh",
DictionaryName:"qwerty",
DictionaryName: "qwerty",
}
entropy := SpatialEntropy(matchPlain, 0, 0)
assert.Equal(t, 9.754887502163468, entropy)
@ -36,7 +35,7 @@ func TestSpatialEntropyCalculation(t *testing.T) {
I: 0,
J: 5,
Token: "asdFgh",
DictionaryName:"qwerty",
DictionaryName: "qwerty",
}
entropyShift := SpatialEntropy(matchShift, 0, 1)
assert.Equal(t, 12.562242424221072, entropyShift)
@ -46,7 +45,7 @@ func TestSpatialEntropyCalculation(t *testing.T) {
I: 0,
J: 5,
Token: "asdcxz",
DictionaryName:"qwerty",
DictionaryName: "qwerty",
}
entropyTurn := SpatialEntropy(matchTurn, 2, 0)
assert.Equal(t, 14.080500893768884, entropyTurn)

View File

@ -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)
}
@ -97,17 +95,18 @@ type DateMatchCandidateTwo struct {
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})
}
}

View File

@ -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"
)

View File

@ -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)
}
}

View File

@ -74,6 +74,4 @@ func loadFrequencyList() {
MATCHERS = append(MATCHERS, dateSepMatcher)
MATCHERS = append(MATCHERS, dateWithoutSepMatch)
}

View File

@ -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,15 +144,13 @@ 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){
func TestPermutationsOfLeetSubstitutions(t *testing.T) {
password := "p4ssw0rd" //[passw0rd, password, p4ssword]
possibleSubs := relevantL33tSubtable(password)
@ -174,14 +170,13 @@ func TestPermutationsOfLeetSubstitutions(t *testing.T){
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)

View File

@ -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

View File

@ -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,

View File

@ -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
}

View File

@ -7,7 +7,6 @@ import (
"time"
)
func PasswordStrength(password string, userInputs []string) scoring.MinEntropyMatch {
start := time.Now()
matches := matching.Omnimatch(password, userInputs)

View File

@ -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))