-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkcdpass.go
58 lines (52 loc) · 1.21 KB
/
xkcdpass.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bufio"
"crypto/rand"
"flag"
"fmt"
"log"
"math/big"
"os"
"strings"
)
func randi(max int) int {
randomNumber, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
log.Fatal(err)
}
return int(randomNumber.Int64())
}
func readWords(path string, min int, max int) []string {
words := []string{}
dict, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer dict.Close()
scanner := bufio.NewScanner(dict)
for scanner.Scan() {
word := scanner.Text()
if len(word) > min && len(word) < max {
words = append(words, word)
}
}
return words
}
// https://xkcd.com/936/
// Generates password from local dictionary
func main() {
dictionaryPath := flag.String("dict", "/usr/share/dict/words", "Path to a custom dictionary")
wordSeparation := flag.String("ws", " ", "String to separate words")
min := flag.Int("min", 5, "minimum word length")
max := flag.Int("max", 9, "maximum word length")
flag.Parse()
words := readWords(*dictionaryPath, *min, *max)
if len(words) < 1000 {
log.Fatal("Dictionary too small.")
}
pass := []string{}
for n := 0; n < 4; n++ {
pass = append(pass, words[randi(len(words))])
}
fmt.Println(strings.Join(pass, *wordSeparation))
}