x/cmd/pwgen.go

30 lines
526 B
Go
Raw Normal View History

2020-10-11 14:50:18 +00:00
package cmd
import (
2020-10-13 18:45:57 +00:00
"crypto/rand"
"fmt"
"math/big"
2020-10-11 14:50:18 +00:00
"github.com/spf13/cobra"
2020-10-13 18:45:57 +00:00
"sneak.berlin/x/pkg/bip39wordlist"
2020-10-11 14:50:18 +00:00
)
func init() {
2020-10-13 18:45:57 +00:00
generateCommand.AddCommand(passwordCmd)
2020-10-11 14:50:18 +00:00
}
2020-10-13 18:45:57 +00:00
var passwordCmd = &cobra.Command{
Use: "password",
2020-10-11 14:50:18 +00:00
Short: "Print a randomly generated password",
Run: func(cmd *cobra.Command, args []string) {
2020-10-13 18:45:57 +00:00
fmt.Printf("%s\n", randomPassword())
2020-10-11 14:50:18 +00:00
},
}
2020-10-13 18:45:57 +00:00
func randomPassword() string {
wl := bip39wordlist.WordListEnglish
count := len(wl)
r, err := rand.Int(rand.Reader, big.Int(count))
return wl[r]
}