72 lines
1.3 KiB
Go
72 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"fmt"
|
|
)
|
|
|
|
func usage(code int, err error) {
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
}
|
|
fmt.Fprintf(os.Stderr, "usage: %s [output_path]\n", os.Args[0])
|
|
os.Exit(code)
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) != 2 {
|
|
usage(0, nil)
|
|
}
|
|
output_path := os.Args[1]
|
|
|
|
private_file, err := os.Create(output_path)
|
|
if err != nil {
|
|
usage(1, err)
|
|
}
|
|
|
|
public_file, err := os.Create(fmt.Sprintf("%s.pub", output_path))
|
|
if err != nil {
|
|
usage(2, err)
|
|
}
|
|
|
|
public, private, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
usage(3, err)
|
|
}
|
|
|
|
public_bytes, err := x509.MarshalPKIXPublicKey(public)
|
|
if err != nil {
|
|
usage(4, err)
|
|
}
|
|
|
|
private_bytes, err := x509.MarshalPKCS8PrivateKey(private)
|
|
if err != nil {
|
|
usage(5, err)
|
|
}
|
|
|
|
err = pem.Encode(public_file, &pem.Block{
|
|
Type: "PUBLIC KEY",
|
|
Bytes: public_bytes,
|
|
})
|
|
|
|
err = pem.Encode(private_file, &pem.Block{
|
|
Type: "PRIVATE KEY",
|
|
Bytes: private_bytes,
|
|
})
|
|
err = public_file.Close()
|
|
if err != nil {
|
|
usage(6, err)
|
|
}
|
|
|
|
err = private_file.Close()
|
|
if err != nil {
|
|
usage(7, err)
|
|
}
|
|
fmt.Printf("Wrote %s and %s.pub\n", output_path, output_path)
|
|
}
|
|
|