-
Notifications
You must be signed in to change notification settings - Fork 18
/
cmd_gen_password.go
79 lines (66 loc) · 1.87 KB
/
cmd_gen_password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"crypto/rand"
"fmt"
"strings"
"github.com/jessevdk/go-flags"
"github.com/kkdai/bstream"
"github.com/lightningnetwork/lnd/aezeed"
)
const (
defaultNumMnemonicWords = 8
defaultPasswordEntropyBits = aezeed.BitsPerWord * defaultNumMnemonicWords
defaultPasswordEntropyBytes = defaultPasswordEntropyBits / 8
)
type jsonPassword struct {
Password string `json:"password"`
}
type genPasswordCommand struct {
Output string `long:"output" short:"o" description:"Output format" choice:"raw" choice:"json"`
}
func newGenPasswordCommand() *genPasswordCommand {
return &genPasswordCommand{
Output: outputFormatRaw,
}
}
func (x *genPasswordCommand) Register(parser *flags.Parser) error {
_, err := parser.AddCommand(
"gen-password",
"Generate a strong password",
"Generate a strong password with 11 bytes of entropy and "+
"print it to stdout, either as raw text or "+
"formatted as JSON",
x,
)
return err
}
func (x *genPasswordCommand) Execute(_ []string) error {
// Read a few bytes of random entropy.
var password [defaultPasswordEntropyBytes]byte
if _, err := rand.Read(password[:]); err != nil {
return fmt.Errorf("unable get password entropy: %v", err)
}
// Then turn the password bytes into a human readable password by using
// the aezeed default mnemonic wordlist.
cipherBits := bstream.NewBStreamReader(password[:])
passwordWords := make([]string, defaultNumMnemonicWords)
for i := 0; i < defaultNumMnemonicWords; i++ {
index, err := cipherBits.ReadBits(aezeed.BitsPerWord)
if err != nil {
return err
}
passwordWords[i] = aezeed.DefaultWordList[index]
}
passwordString := strings.Join(passwordWords, "-")
if x.Output == outputFormatJSON {
var err error
passwordString, err = asJSON(&jsonPassword{
Password: passwordString,
})
if err != nil {
return err
}
}
fmt.Printf("%s", passwordString)
return nil
}