pgp-key-management/keys/generate-keys.go

99 lines
1.6 KiB
Go
Raw Normal View History

2024-08-04 14:34:41 -07:00
package main
import (
//"crypto/rand"
//"crypto/rsa"
//"crypto/x509"
//"encoding/pem"
2024-08-04 14:34:41 -07:00
"fmt"
"os"
"path/filepath"
//"bytes"
2024-08-04 14:34:41 -07:00
//"io"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/go-crypto/openpgp/armor"
2024-08-04 14:34:41 -07:00
)
func main() {
err := run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type Ident struct {
Name, Comment, Email string
}
2024-08-04 14:34:41 -07:00
func run() error {
idents := []Ident{
{"Company", "", "main@company.com"},
{"Customer", "", "customer@example.com"},
2024-08-04 14:34:41 -07:00
}
for _, ident := range idents {
fmt.Println("Generating keypair for", ident.Name)
err := keypair(ident)
2024-08-04 14:34:41 -07:00
if err != nil {
return err
}
}
return nil
}
const (
keyDir string = "./"
)
func keypair(ident Ident) error {
ent, err := openpgp.NewEntity(ident.Name, ident.Comment, ident.Email, &packet.Config{
RSABits: 4096,
Algorithm: packet.PubKeyAlgoRSA,
})
2024-08-04 14:34:41 -07:00
if err != nil {
return err
}
// Public
pubOut, err := os.Create(filepath.Join(keyDir, "public", ident.Name+".asc"))
2024-08-04 14:34:41 -07:00
if err != nil {
return err
}
defer pubOut.Close()
2024-08-04 14:34:41 -07:00
pubWriter, err := armor.Encode(pubOut, "PGP PUBLIC KEY BLOCK", nil)
2024-08-04 14:34:41 -07:00
if err != nil {
return err
}
defer pubWriter.Close()
2024-08-04 14:34:41 -07:00
err = ent.Serialize(pubWriter)
2024-08-04 14:34:41 -07:00
if err != nil {
return err
}
// Private
privOut, err := os.Create(filepath.Join(keyDir, "private", ident.Name+".asc"))
if err != nil {
return err
}
defer privOut.Close()
2024-08-04 14:34:41 -07:00
privWriter, err := armor.Encode(privOut, "PGP PRIVATE KEY BLOCK", nil)
2024-08-04 14:34:41 -07:00
if err != nil {
return err
}
defer privWriter.Close()
2024-08-04 14:34:41 -07:00
err = ent.SerializePrivate(privWriter, nil)
if err != nil {
return err
}
2024-08-04 14:34:41 -07:00
return nil
}