Skip to content

Commit

Permalink
age-keygen: Add opt to compute pubkey from privkey
Browse files Browse the repository at this point in the history
  • Loading branch information
dsprenkels committed Sep 29, 2020
1 parent 31500bf commit a36771d
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions cmd/age-keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
log.SetFlags(0)

outFlag := flag.String("o", "", "output to `FILE` (default stdout)")
pubkeyFlag := flag.Bool("pubkey", false, "Read the private key file from standard input and print the corresponding public key.")
flag.Parse()
if len(flag.Args()) != 0 {
log.Fatalf("age-keygen takes no arguments")
Expand All @@ -36,17 +37,22 @@ func main() {
out = f
}

if *pubkeyFlag {
in := os.Stdin
generatePubkey(out, in)
} else {
generatePrivkey(out)
}
}

func generatePrivkey(out *os.File) {
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"+
"Consider setting the umask to 066 and trying again.\n")
}
}

generate(out)
}

func generate(out *os.File) {
k, err := age.GenerateX25519Identity()
if err != nil {
log.Fatalf("Internal error: %v", err)
Expand All @@ -60,3 +66,23 @@ func generate(out *os.File) {
fmt.Fprintf(out, "# public key: %s\n", k.Recipient())
fmt.Fprintf(out, "%s\n", k)
}

func generatePubkey(out *os.File, in *os.File) {
ids, err := age.ParseIdentities(in)
if err != nil {
log.Fatalf("failed to read %q: %v", in.Name(), err)
}
if len(ids) == 0 {
log.Fatalln("no identities found in input")
} else if len(ids) > 1 {
log.Fatalln("more than one identity provided in input")
}
id := ids[0]

k, ok := id.(*age.X25519Identity)
if !ok {
log.Fatalf("identity is not an X25519 identity (but %q)", id.Type())
}

fmt.Fprintf(out, "%s\n", k.Recipient())
}

0 comments on commit a36771d

Please sign in to comment.