From 08420e90c11754d52ab02a9b4c358dfa3b86c9e5 Mon Sep 17 00:00:00 2001 From: Sylvia Moss Date: Thu, 25 Feb 2021 22:01:33 +0100 Subject: [PATCH] Add random number and --remove-number flag --- README.md | 1 + cmd/root.go | 1 + diceware/diceware.go | 28 +++++++++++++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b463946f..10da9425 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Flags: --lang string password language available langs: en, pt (default "en") --lower remove capitalized first letters + --remove-number removes the random number we add by default --separator string character that separates the words. use --separator=none to remove reparator (default "/") --size int32 the amount words the password will have (default 6) diff --git a/cmd/root.go b/cmd/root.go index 70369e38..c7f9f68b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,6 +45,7 @@ func Execute() { generateCmd.Flags().BoolVar(&generateConfig.Pbcopy, "copy", false, "pbcopy password") generateCmd.Flags().BoolVar(&generateConfig.Hide, "hide", false, "pbcopy and hide password. Password WON'T be printed out") generateCmd.Flags().BoolVar(&generateConfig.Lower, "lower", false, "remove capitalized first letters") + generateCmd.Flags().BoolVar(&generateConfig.RemoveNumber, "remove-number", false, "removes the random number we add by default") rootCmd.AddCommand(generateCmd) configCmd.Flags().BoolVar(&customConfig.AddLang, "add-lang", false, "add new config language") diff --git a/diceware/diceware.go b/diceware/diceware.go index 9f2f7da0..a91fac50 100644 --- a/diceware/diceware.go +++ b/diceware/diceware.go @@ -19,12 +19,13 @@ import ( ) type GenerateConfig struct { - Lang string - Size int32 - Pbcopy bool - Hide bool - Lower bool - Separator string + Lang string + Size int32 + Pbcopy bool + Hide bool + Lower bool + RemoveNumber bool + Separator string } //go:embed words @@ -36,8 +37,13 @@ func (c *GenerateConfig) Generate() error { separator = "" } + numberedIndex, err := rand.Int(rand.Reader, big.NewInt(int64(c.Size))) + if err != nil { + return err + } + var words string - for i := 1; i <= int(c.Size); i++ { + for i := 0; i < int(c.Size); i++ { index, err := findDicewareWordIndex() if err != nil { return err @@ -46,6 +52,14 @@ func (c *GenerateConfig) Generate() error { if err != nil { return err } + if numberedIndex.Int64() == int64(i) && !c.RemoveNumber { + randomNumber, err := rand.Int(rand.Reader, big.NewInt(int64(10))) + if err != nil { + return err + } + words = fmt.Sprintf("%s%s%d%s", words, word, randomNumber, separator) + continue + } words = words + word + separator } words = words[:len(words)-len(separator)]