diff options
author | Michaël Ball <michael.ball@gmail.com> | 2016-09-04 19:14:15 +0100 |
---|---|---|
committer | Michaël Ball <michael.ball@gmail.com> | 2016-09-04 19:14:15 +0100 |
commit | 21eb15e87b42728d6fd3de6cbc256dde62fbacfd (patch) | |
tree | 690a960abbde59f4d3110c57777cd474d8109d5a | |
parent | 43ed07477ae8e7d8da6628b9e7e013f6400e7a1b (diff) |
Add source
-rw-r--r-- | passphrasegen.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/passphrasegen.go b/passphrasegen.go new file mode 100644 index 0000000..32d708e --- /dev/null +++ b/passphrasegen.go @@ -0,0 +1,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, " ")) +} |