1
0
Fork 0

Initial commit

master
Ambrose Chua 2017-07-17 12:16:19 +08:00
parent 6d88610bbe
commit fcf752121d
7 changed files with 9221 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
pw/words.go
pword

View File

@ -25,7 +25,7 @@ NAME:
pword - generate secure passwords
USAGE:
greet command [options]
pword command [options]
COMMANDS:
help, h Shows a list of commands or help for one command

7776
pw/eff-long.txt Normal file

File diff suppressed because it is too large Load Diff

1296
pw/eff-short.txt Normal file

File diff suppressed because it is too large Load Diff

64
pw/pw.go Normal file
View File

@ -0,0 +1,64 @@
package pw
//go:generate go run words_generate.go
import (
"log"
"strings"
"crypto/rand"
"math/big"
)
type Strength int
const (
StrengthOnline Strength = 4
StrengthOffline Strength = 6
StrengthCrypto Strength = 8
)
type Mode int
const (
ModeShort Mode = iota
ModeLong
)
type Generator struct {
Mode
Strength
}
func NewGenerator(m Mode, s Strength) Generator {
return Generator{
Mode: m,
Strength: s,
}
}
func (g Generator) Next() string {
n := int(g.Strength)
s := []string{}
for i := 0; i < n; i++ {
s = append(s, g.GenerateWord());
}
return strings.Join(s, " ")
}
func (g Generator) GenerateWord() string {
var wordsList []string
if g.Mode == ModeShort {
wordsList = WordsShort
}
if g.Mode == ModeLong {
wordsList = WordsLong
}
c := len(wordsList)
i, err := rand.Int(rand.Reader, big.NewInt(int64(c)))
if err != nil {
log.Fatal(err)
}
return wordsList[i.Int64()]
}

70
pw/words_generate.go Normal file
View File

@ -0,0 +1,70 @@
// +build ignore
package main
import (
"log"
"os"
"bufio"
"text/template"
"time"
)
func main() {
wordsShort := loadWords("eff-short.txt")
wordsLong := loadWords("eff-long.txt")
f, err := os.Create("words.go")
if err != nil {
log.Fatal(err)
}
defer f.Close()
packageTemplate.Execute(f, struct {
Timestamp time.Time
WordsShort []string
WordsLong []string
}{
Timestamp: time.Now(),
WordsShort: wordsShort,
WordsLong: wordsLong,
})
}
func loadWords(filename string) (words []string) {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
sc := bufio.NewScanner(f)
sc.Split(bufio.ScanWords)
for sc.Scan() {
sc.Scan()
words = append(words, sc.Text())
}
if sc.Err() != nil {
log.Fatal(err)
}
return
}
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// {{ .Timestamp }}
package pw
var WordsShort = []string{
{{- range .WordsShort }}
{{ printf "%q" . }},
{{- end }}
}
var WordsLong = []string{
{{- range .WordsLong }}
{{ printf "%q" . }},
{{- end }}
}
`))

12
pword.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"fmt"
"github.com/serverwentdown/pword/pw"
)
func main() {
g := pw.NewGenerator(pw.ModeShort, pw.StrengthOnline)
fmt.Println(g.Next())
}