Skip to content

Commit

Permalink
Add random number and --remove-number flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss committed Feb 25, 2021
1 parent 991dfb9 commit 08420e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 21 additions & 7 deletions diceware/diceware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)]
Expand Down

0 comments on commit 08420e9

Please sign in to comment.