Skip to content

Commit

Permalink
feat: Add EFF short wordlists
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 11, 2024
1 parent 53e3d7e commit c539b92
Show file tree
Hide file tree
Showing 12 changed files with 2,718 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Command line passphrase generator written in Go.

The [EFF Long Wordlist](https://www.eff.org/dice) is embedded which includes 7776 words. See the [EFF's Deep Dive](https://www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases) for more details on the benefits of this word list.
The [EFF Diceware Wordlists](https://www.eff.org/dice) are embedded, with the long wordlist used by default. See the [EFF's Deep Dive](https://www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases) for more details on the benefits of this word list.

## Installation

Expand Down
7 changes: 6 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ func New(version, commit string) *cobra.Command {
}

cfg, _ := config.GetFilePretty()
defaultCfg := config.NewDefault()
cmd.PersistentFlags().String("config", "", "Config file (default "+cfg+")")
cmd.PersistentFlags().IntP("count", "c", config.NewDefault().Count, "Number of passphrases to generate")
cmd.PersistentFlags().IntP("count", "c", defaultCfg.Count, "Number of passphrases to generate")
cmd.PersistentFlags().String("wordlist", "long", "Wordlist to use (one of: long, short1, short2)")
_ = cmd.RegisterFlagCompletionFunc("wordlist", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{config.WordlistLong, config.WordlistShort1, config.WordlistShort2}, cobra.ShellCompDirectiveNoFileComp
})

template := template.New()
cmd.AddCommand(
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("missing config")
}

tmpl, err := template.New("").Funcs(pwgen_template.FuncMap()).Parse(conf.Template)
tmpl, err := template.New("").Funcs(pwgen_template.FuncMap(conf)).Parse(conf.Template)
if err != nil {
return fmt.Errorf("invalid format: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions docs/pwgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ Generate passphrases
### Options

```
--config string Config file (default $HOME/.config/pwgen-go/config.toml)
-c, --count int Number of passphrases to generate (default 10)
-h, --help help for pwgen
--config string Config file (default $HOME/.config/pwgen-go/config.toml)
-c, --count int Number of passphrases to generate (default 10)
-h, --help help for pwgen
--wordlist string Wordlist to use (one of: long, short1, short2) (default "long")
```

### SEE ALSO
Expand Down
5 changes: 3 additions & 2 deletions docs/pwgen_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pwgen template [flags]
### Options inherited from parent commands

```
--config string Config file (default $HOME/.config/pwgen-go/config.toml)
-c, --count int Number of passphrases to generate (default 10)
--config string Config file (default $HOME/.config/pwgen-go/config.toml)
-c, --count int Number of passphrases to generate (default 10)
--wordlist string Wordlist to use (one of: long, short1, short2) (default "long")
```

### SEE ALSO
Expand Down
8 changes: 8 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ package config
type Config struct {
Count int `toml:"count" comment:"Number of passphrases to generate."`
Template string `toml:"template" comment:"Template used to generate passphrases."`
Wordlist string `toml:"wordlist" comment:"Wordlist to use. (one of: long, short1, short2)"`
}

const (
WordlistLong = "long"
WordlistShort1 = "short1"
WordlistShort2 = "short2"
)

func NewDefault() *Config {
return &Config{
Count: 10,
Template: `{{ wordsWithNumber 3 | join "-" | title }}`,
Wordlist: WordlistLong,
}
}
7 changes: 7 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
var flagConfigTable = map[string]string{
"count": "count",
"template": "template",
"wordlist": "wordlist",
}

func Load(cmd *cobra.Command) (*Config, error) {
Expand Down Expand Up @@ -96,5 +97,11 @@ func Load(cmd *cobra.Command) (*Config, error) {
return nil, err
}

switch conf.Wordlist {
case WordlistLong, WordlistShort1, WordlistShort2:
default:
conf.Wordlist = WordlistLong
}

return conf, err
}
27 changes: 19 additions & 8 deletions internal/template/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@ import (
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/gabe565/pwgen-go/internal/config"
"github.com/gabe565/pwgen-go/internal/rand"
"github.com/gabe565/pwgen-go/internal/wordlist"
)

func FuncMap() template.FuncMap {
func FuncMap(conf *config.Config) template.FuncMap {
funcs := sprig.FuncMap()

funcs["randWord"] = wordlist.EFF_Long.RandWord
funcs["word"] = wordlist.EFF_Long.RandWord

funcs["randWords"] = wordlist.EFF_Long.RandWords
funcs["words"] = wordlist.EFF_Long.RandWords
funcs["wordsWithNumber"] = wordlist.EFF_Long.RandWordsWithNumber
funcs["wordsWithNum"] = wordlist.EFF_Long.RandWordsWithNumber
var list wordlist.Wordlist
switch conf.Wordlist {
case config.WordlistLong:
list = wordlist.EFF_Long
case config.WordlistShort1:
list = wordlist.EFF_Short1
case config.WordlistShort2:
list = wordlist.EFF_Short2
}

funcs["randWord"] = list.RandWord
funcs["word"] = list.RandWord

funcs["randWords"] = list.RandWords
funcs["words"] = list.RandWords
funcs["wordsWithNumber"] = list.RandWordsWithNumber
funcs["wordsWithNum"] = list.RandWordsWithNumber

funcs["number"] = funcs["randNumeric"]
funcs["num"] = funcs["randNumeric"]
Expand Down
9 changes: 6 additions & 3 deletions internal/wordlist/eff_long.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c539b92

Please sign in to comment.