From f7340d2ccda6aeda51ca9a6c134496b314358922 Mon Sep 17 00:00:00 2001 From: Michaël Ball Date: Mon, 5 Sep 2016 21:53:07 +0100 Subject: Add changelog --- CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGELOG diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..94464bc --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,3 @@ +0.1.0 + +Initial release. \ No newline at end of file -- cgit v1.2.3 From ed0b45cfce6d54924002ef005795f3c45b808dac Mon Sep 17 00:00:00 2001 From: Michaël Ball Date: Thu, 8 Sep 2016 17:20:54 +0100 Subject: Move generation to library --- generator/generator.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ generator/generator_test.go | 15 +++++++++++++++ passphrasegen.go | 35 ++++------------------------------- 3 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 generator/generator.go create mode 100644 generator/generator_test.go diff --git a/generator/generator.go b/generator/generator.go new file mode 100644 index 0000000..3ec5154 --- /dev/null +++ b/generator/generator.go @@ -0,0 +1,45 @@ +package generator + +import ( + "bufio" + "math/rand" + "os" + "time" +) + +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() +} + +// RandomWords picks numberOfWords random words from the file located at +// wordsFile +func RandomWords(wordsFile string, numberOfWords int) ([]string, error) { + rand.Seed(time.Now().UTC().UnixNano()) + + words, err := readLines(wordsFile) + + if err != nil { + return nil, err + } + + phraseWords := make([]string, numberOfWords) + + for index := 0; index < numberOfWords; index++ { + randInt := rand.Intn((len(words) - 1)) + + phraseWords[index] = words[randInt] + } + + return phraseWords, nil +} diff --git a/generator/generator_test.go b/generator/generator_test.go new file mode 100644 index 0000000..4cda593 --- /dev/null +++ b/generator/generator_test.go @@ -0,0 +1,15 @@ +package generator + +import "testing" + +func TestRandomWords(t *testing.T) { + words, err := RandomWords("/usr/share/dict/words", 4) + + if err != nil { + t.Errorf("%s", err) + } + + if len(words) != 4 { + t.Errorf("RandomWords does not return the correct number of words") + } +} diff --git a/passphrasegen.go b/passphrasegen.go index 32d708e..89b25d2 100644 --- a/passphrasegen.go +++ b/passphrasegen.go @@ -1,39 +1,20 @@ package main import ( - "bufio" "flag" "fmt" - "math/rand" - "os" "strings" - "time" + + generator "github.com/michael-ball/passphrasegen/generator" ) 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()) + flag.Parse() } func main() { - flag.Parse() - args := flag.Args() wordsFile := "/usr/share/dict/words" @@ -42,20 +23,12 @@ func main() { wordsFile = args[0] } - words, err := readLines(wordsFile) + phraseWords, err := generator.RandomWords(wordsFile, *numWords) 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 From 898acf05e9298b910c17e7bb4b5a98245cdcf9b4 Mon Sep 17 00:00:00 2001 From: Michaël Ball Date: Sun, 16 Oct 2016 11:35:06 +0100 Subject: Add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ee6649 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +passphrasegen \ No newline at end of file -- cgit v1.2.3 From 7e0c7250e1a9afd9930fbb200c5cbd9a8095404e Mon Sep 17 00:00:00 2001 From: Michaël Ball Date: Sat, 7 Jan 2017 18:07:23 +0000 Subject: Update changelog --- CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 94464bc..630c2c1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +0.2.0 +Add tests, refactor code + 0.1.0 Initial release. \ No newline at end of file -- cgit v1.2.3