aboutsummaryrefslogtreecommitdiff
path: root/passphrasegen.go
blob: 32d708e26521eb20cf600a3d4f4e2a84d832860a (plain)
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
59
60
61
package main

import (
	"bufio"
	"flag"
	"fmt"
	"math/rand"
	"os"
	"strings"
	"time"
)

var numWords = flag.Int("w", 4, "Number of words in passphrase")

func readLines(path string) ([]string, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	var lines []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	return lines, scanner.Err()
}

func init() {
	rand.Seed(time.Now().UTC().UnixNano())
}

func main() {
	flag.Parse()

	args := flag.Args()

	wordsFile := "/usr/share/dict/words"

	if len(args) > 0 {
		wordsFile = args[0]
	}

	words, err := readLines(wordsFile)

	if err != nil {
		fmt.Println(err)
		return
	}

	phraseWords := make([]string, *numWords)

	for index := 0; index < *numWords; index++ {
		randInt := rand.Intn((len(words) - 1))

		phraseWords[index] = words[randInt]
	}

	fmt.Println(strings.Join(phraseWords, " "))
}