From 21eb15e87b42728d6fd3de6cbc256dde62fbacfd Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Sun, 4 Sep 2016 19:14:15 +0100 Subject: Add source --- passphrasegen.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 passphrasegen.go 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, " ")) +} -- cgit v1.2.3