cmd/age: offer to autogenerate a secure passphrase

As suggested by @tqbf: https://twitter.com/tqbf/status/1210715916381511680
This commit is contained in:
Filippo Valsorda
2019-12-30 00:16:08 +01:00
parent b3fc5d19ea
commit 80c6b4edd9
2 changed files with 49 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ import (
"io"
_log "log"
"os"
"strings"
"filippo.io/age/internal/age"
"golang.org/x/crypto/ssh/terminal"
@@ -147,7 +148,7 @@ func main() {
case decryptFlag:
decrypt(identityFlags, in, out)
case passFlag:
pass, err := passphrasePrompt()
pass, err := passphrasePromptWithDefault()
if err != nil {
logFatalf("Error: %v", err)
}
@@ -157,13 +158,22 @@ func main() {
}
}
func passphrasePrompt() (string, error) {
fmt.Fprintf(os.Stderr, "Enter passphrase: ")
func passphrasePromptWithDefault() (string, error) {
fmt.Fprintf(os.Stderr, "Enter passphrase (leave empty to autogenerate a secure one): ")
pass, err := readPassphrase()
if err != nil {
return "", fmt.Errorf("could not read passphrase: %v", err)
}
return string(pass), nil
p := string(pass)
if p == "" {
var words []string
for i := 0; i < 10; i++ {
words = append(words, randomWord())
}
p = strings.Join(words, "-")
fmt.Fprintf(os.Stderr, "Using the autogenerated passphrase %q.\n", p)
}
return p, nil
}
func encryptKeys(keys []string, in io.Reader, out io.Writer, armor bool) {
@@ -229,6 +239,15 @@ func decrypt(keys []string, in io.Reader, out io.Writer) {
}
}
func passphrasePrompt() (string, error) {
fmt.Fprintf(os.Stderr, "Enter passphrase: ")
pass, err := readPassphrase()
if err != nil {
return "", fmt.Errorf("could not read passphrase: %v", err)
}
return string(pass), nil
}
func logFatalf(format string, v ...interface{}) {
_log.Printf(format, v...)
_log.Fatalf("[ Did age not do what you expected? Could an error be more useful?" +

26
cmd/age/wordlist.go Normal file

File diff suppressed because one or more lines are too long