2015-09-29 22:35:25 +00:00
|
|
|
package adjacency
|
2016-01-13 22:49:04 +00:00
|
|
|
|
2015-09-29 22:35:25 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-01-13 22:49:04 +00:00
|
|
|
"log"
|
|
|
|
// "fmt"
|
2015-10-07 00:14:08 +00:00
|
|
|
"github.com/nbutton23/zxcvbn-go/data"
|
2015-09-29 22:35:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AdjacencyGraph struct {
|
2016-01-13 22:49:04 +00:00
|
|
|
Graph map[string][]string
|
2015-10-06 18:43:37 +00:00
|
|
|
averageDegree float64
|
2016-01-13 22:49:04 +00:00
|
|
|
Name string
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 22:49:04 +00:00
|
|
|
var AdjacencyGph = make(map[string]AdjacencyGraph)
|
2015-10-05 19:56:21 +00:00
|
|
|
|
2015-10-07 00:14:08 +00:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Lshortfile)
|
2015-10-14 15:38:47 +00:00
|
|
|
AdjacencyGph["qwerty"] = BuildQwerty()
|
|
|
|
AdjacencyGph["dvorak"] = BuildDvorak()
|
|
|
|
AdjacencyGph["keypad"] = BuildKeypad()
|
|
|
|
AdjacencyGph["macKeypad"] = BuildMacKeypad()
|
2016-01-28 00:11:50 +00:00
|
|
|
AdjacencyGph["l33t"] = BuildLeet()
|
2015-10-07 00:14:08 +00:00
|
|
|
}
|
2015-10-05 19:56:21 +00:00
|
|
|
|
2015-10-14 15:38:47 +00:00
|
|
|
func BuildQwerty() AdjacencyGraph {
|
2015-10-07 00:14:08 +00:00
|
|
|
data, err := zxcvbn_data.Asset("data/Qwerty.json")
|
|
|
|
if err != nil {
|
|
|
|
panic("Can't find asset")
|
|
|
|
}
|
|
|
|
return GetAdjancencyGraphFromFile(data, "qwerty")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
2015-10-14 15:38:47 +00:00
|
|
|
func BuildDvorak() AdjacencyGraph {
|
2015-10-07 00:14:08 +00:00
|
|
|
data, err := zxcvbn_data.Asset("data/Dvorak.json")
|
|
|
|
if err != nil {
|
|
|
|
panic("Can't find asset")
|
|
|
|
}
|
|
|
|
return GetAdjancencyGraphFromFile(data, "dvorak")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
2015-10-14 15:38:47 +00:00
|
|
|
func BuildKeypad() AdjacencyGraph {
|
2015-10-07 00:14:08 +00:00
|
|
|
data, err := zxcvbn_data.Asset("data/Keypad.json")
|
|
|
|
if err != nil {
|
|
|
|
panic("Can't find asset")
|
|
|
|
}
|
|
|
|
return GetAdjancencyGraphFromFile(data, "keypad")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
2015-10-14 15:38:47 +00:00
|
|
|
func BuildMacKeypad() AdjacencyGraph {
|
2015-10-07 00:14:08 +00:00
|
|
|
data, err := zxcvbn_data.Asset("data/MacKeypad.json")
|
2015-09-29 22:35:25 +00:00
|
|
|
if err != nil {
|
2015-10-07 00:14:08 +00:00
|
|
|
panic("Can't find asset")
|
2015-09-29 22:35:25 +00:00
|
|
|
}
|
2015-10-07 00:14:08 +00:00
|
|
|
return GetAdjancencyGraphFromFile(data, "mac_keypad")
|
|
|
|
}
|
2016-01-28 00:11:50 +00:00
|
|
|
func BuildLeet() AdjacencyGraph {
|
|
|
|
data, err := zxcvbn_data.Asset("data/L33t.json")
|
|
|
|
if err != nil {
|
|
|
|
panic("Can't find asset")
|
|
|
|
}
|
|
|
|
return GetAdjancencyGraphFromFile(data, "keypad")
|
|
|
|
}
|
2015-09-29 22:35:25 +00:00
|
|
|
|
2015-10-07 00:14:08 +00:00
|
|
|
func GetAdjancencyGraphFromFile(data []byte, name string) AdjacencyGraph {
|
2015-09-29 22:35:25 +00:00
|
|
|
|
2016-01-13 22:49:04 +00:00
|
|
|
var graph AdjacencyGraph
|
2015-10-07 00:14:08 +00:00
|
|
|
err := json.Unmarshal(data, &graph)
|
2015-09-29 22:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2015-10-05 19:56:21 +00:00
|
|
|
graph.Name = name
|
2015-09-29 22:35:25 +00:00
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
2015-10-01 13:38:42 +00:00
|
|
|
//on qwerty, 'g' has degree 6, being adjacent to 'ftyhbv'. '\' has degree 1.
|
|
|
|
//this calculates the average over all keys.
|
|
|
|
//TODO double check that i ported this correctly scoring.coffee ln 5
|
2016-01-13 22:49:04 +00:00
|
|
|
func (adjGrp AdjacencyGraph) CalculateAvgDegree() float64 {
|
2015-10-06 18:43:37 +00:00
|
|
|
if adjGrp.averageDegree != float64(0) {
|
2015-10-05 19:56:21 +00:00
|
|
|
return adjGrp.averageDegree
|
|
|
|
}
|
2015-10-06 18:43:37 +00:00
|
|
|
var avg float64
|
|
|
|
var count float64
|
2015-10-01 13:38:42 +00:00
|
|
|
for _, value := range adjGrp.Graph {
|
|
|
|
|
|
|
|
for _, char := range value {
|
|
|
|
if char != "" || char != " " {
|
2015-10-06 18:43:37 +00:00
|
|
|
avg += float64(len(char))
|
2015-10-01 13:38:42 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:49:04 +00:00
|
|
|
adjGrp.averageDegree = avg / count
|
2015-10-05 19:56:21 +00:00
|
|
|
|
|
|
|
return adjGrp.averageDegree
|
2015-10-01 13:38:42 +00:00
|
|
|
}
|