age/cmd/age-keygen/keygen.go
Filippo Valsorda 9a84e437b1 all: switch key format to Bech32
Use the BIP173 format, which is whole-word selectable, markup safe, and
case insensitive.

AGE-SECRET-KEY-1FPSHVEFQXYSX5MMFDE6ZCGRTV4JHQGRFWSS8WETVDSSX76TVV4JQU272CR

See https://groups.google.com/d/msg/age-dev/UAjkvLoCr9I/l4Q1h3OPAgAJ.

All bech32 Go packages have funky APIs, internal types, or case
handling, so include a heavily refactored version of the reference
implementation, and the tests from github.com/btcsuite/btcutil/bech32.
2019-12-27 17:13:20 +01:00

58 lines
1.3 KiB
Go

// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"time"
"filippo.io/age/internal/age"
)
func main() {
log.SetFlags(0)
outFlag := flag.String("o", "", "output to `FILE` (default stdout)")
flag.Parse()
if len(flag.Args()) != 0 {
log.Fatalf("age-keygen takes no arguments")
}
out := os.Stdout
if name := *outFlag; name != "" {
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
log.Fatalf("Failed to open output file %q: %v", name, err)
}
defer f.Close()
out = f
}
if fi, err := out.Stat(); err == nil {
if fi.Mode().IsRegular() && fi.Mode().Perm()&0004 != 0 {
fmt.Fprintf(os.Stderr, "Warning: writing to a world-readable file.\n")
fmt.Fprintf(os.Stderr, "Consider setting the umask to 066 and trying again.\n")
}
}
generate(out)
}
func generate(out io.Writer) {
k, err := age.GenerateX25519Identity()
if err != nil {
log.Fatalf("Internal error: %v", err)
}
fmt.Fprintf(out, "# created: %s\n", time.Now().Format(time.RFC3339))
fmt.Fprintf(out, "# public key: %s\n", k.Recipient())
fmt.Fprintf(out, "%s\n", k)
}