make key output behaviour closer to the one in age-keygen

This commit is contained in:
akulij 2025-02-26 21:44:01 +00:00
parent be0aeac776
commit c935f4ba81

View File

@ -35,11 +35,17 @@ func main() {
errorf("internal error: %v", err) errorf("internal error: %v", err)
} }
fmt.Printf("Public key: %s\n", k.Recipient()) // if user is not seeing private keyfile, which also contains public key,
// also duplicate public key it to stderr,
// but if user sees public key via stdout, no need for duplication
if !term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Fprintf(os.Stderr, "Public key: %s\n", k.Recipient())
}
fmt.Printf("# created: %s\n", time.Now().Format(time.RFC3339)) err = writeSecretKey(os.Stdout, k)
fmt.Printf("# public key: %s\n", k.Recipient()) if err != nil {
fmt.Printf("%s\n", k) fmt.Printf("Failed to write secret key to file, error: %s\n", err)
}
} }
func getPasswordBytes() ([]byte, error) { func getPasswordBytes() ([]byte, error) {
@ -53,6 +59,21 @@ func getPasswordBytes() ([]byte, error) {
} }
} }
func writeSecretKey(f *os.File, key *age.X25519Identity) error {
var err error
_, err = fmt.Fprintf(f, "# created: %s\n", time.Now().Format(time.RFC3339))
if err != nil { return err; }
_, err = fmt.Fprintf(f, "# public key: %s\n", key.Recipient())
if err != nil { return err; }
_, err = fmt.Fprintf(f, "%s\n", key)
if err != nil { return err; }
return nil
}
// almost a copy of private function in age/x25519.go // almost a copy of private function in age/x25519.go
func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error) { func newX25519IdentityFromScalar(secretKey []byte) (*age.X25519Identity, error) {
if len(secretKey) != curve25519.ScalarSize { if len(secretKey) != curve25519.ScalarSize {