Initial commit
parent
6d88610bbe
commit
fcf752121d
|
@ -0,0 +1,2 @@
|
||||||
|
pw/words.go
|
||||||
|
pword
|
|
@ -25,7 +25,7 @@ NAME:
|
||||||
pword - generate secure passwords
|
pword - generate secure passwords
|
||||||
|
|
||||||
USAGE:
|
USAGE:
|
||||||
greet command [options]
|
pword command [options]
|
||||||
|
|
||||||
COMMANDS:
|
COMMANDS:
|
||||||
help, h Shows a list of commands or help for one command
|
help, h Shows a list of commands or help for one command
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -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()]
|
||||||
|
}
|
|
@ -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 }}
|
||||||
|
}
|
||||||
|
`))
|
Loading…
Reference in New Issue