passgen

A password and passphrase generator written in Go.
Log | Files | Refs | README | LICENSE

commit 9d7437d23fe1f235371cd22f602e3a821b4953f8
parent de9ae7d486a7bd0a6b099fff12523083469e9d8a
Author: Jaron Swab <jr@jrswab.com>
Date:   Tue, 23 Mar 2021 20:01:43 -0400

Added clipboard support

Now the default functionality.

Diffstat:
Agenerate/common.go | 7+++++++
Mgenerate/phrase.go | 21+++++++++++++++------
Mgo.mod | 5++++-
Mgo.sum | 2++
Mmain.go | 16++++++++++++++--
5 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/generate/common.go b/generate/common.go @@ -0,0 +1,7 @@ +package generate + +type Options struct { + Count uint + Sep string + Stdout bool +} diff --git a/generate/phrase.go b/generate/phrase.go @@ -4,23 +4,32 @@ import ( "fmt" "math/rand" "time" + + "github.com/atotto/clipboard" ) -func Passphrase(c uint, sep string) { - rand.Seed(time.Now().Unix()) +func (o *Options) Passphrase() { + rand.Seed(time.Now().UnixNano()) var ( words = wordlist() phrase string ) - for c > 0 { - c-- + for o.Count > 0 { + o.Count-- num := rand.Intn(7775) if phrase == "" { phrase = fmt.Sprintf("%s%s", phrase, words[num]) continue } - phrase = fmt.Sprintf("%s%s%s", phrase, sep, words[num]) + phrase = fmt.Sprintf("%s%s%s", phrase, o.Sep, words[num]) + } + if o.Stdout { + fmt.Println(phrase) + return + } + + if err := clipboard.WriteAll(phrase); err != nil { + panic(err) } - fmt.Println(phrase) } diff --git a/go.mod b/go.mod @@ -2,4 +2,7 @@ module git.swab.dev/passgen go 1.16 -require github.com/urfave/cli/v2 v2.3.0 // indirect +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/urfave/cli/v2 v2.3.0 // indirect +) diff --git a/go.sum b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/main.go b/main.go @@ -19,7 +19,7 @@ func main() { Aliases: []string{"p"}, Usage: "use the EFF wordlist to create a passphrase", Flags: []cli.Flag{ - &cli.IntFlag{ + &cli.UintFlag{ Name: "number", Aliases: []string{"n"}, Usage: "The total number of words to return.", @@ -33,12 +33,24 @@ func main() { Value: "-", Required: false, }, + &cli.BoolFlag{ + Name: "stdout", + Aliases: []string{"o"}, + Usage: "Set the output location to stdout. (If used, passphrase will not be copied to the clipboard.)", + Value: false, + Required: false, + }, }, Action: func(c *cli.Context) error { if c.Int("number") < 0 { return fmt.Errorf("the number of words must be greater than zero") } - generate.Passphrase(c.Uint("number"), c.String("separator")) + o := &generate.Options{ + Count: c.Uint("number"), + Sep: c.String("separator"), + Stdout: c.Bool("stdout"), + } + o.Passphrase() return nil }, },