2024-08-04 14:34:41 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-04 15:27:29 -07:00
|
|
|
//"crypto/rand"
|
|
|
|
//"crypto/rsa"
|
|
|
|
//"crypto/x509"
|
|
|
|
//"encoding/pem"
|
2024-08-04 14:34:41 -07:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-08-04 15:27:29 -07:00
|
|
|
//"bytes"
|
2024-08-04 14:34:41 -07:00
|
|
|
//"io"
|
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
type Ident struct {
|
|
|
|
Name, Comment, Email string
|
|
|
|
}
|
2024-08-04 14:34:41 -07:00
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
func run() error {
|
|
|
|
idents := []Ident{
|
|
|
|
{"Company", "", "main@company.com"},
|
|
|
|
{"Customer", "", "customer@example.com"},
|
2024-08-04 14:34:41 -07:00
|
|
|
}
|
|
|
|
|
2024-08-04 15:27:29 -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 = "./"
|
|
|
|
)
|
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
// 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
|
|
|
|
}
|
2024-08-04 15:27:29 -07:00
|
|
|
defer pubOut.Close()
|
2024-08-04 14:34:41 -07:00
|
|
|
|
2024-08-04 15:27:29 -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
|
|
|
|
}
|
2024-08-04 15:27:29 -07:00
|
|
|
defer pubWriter.Close()
|
2024-08-04 14:34:41 -07:00
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
err = ent.Serialize(pubWriter)
|
2024-08-04 14:34:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
// 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
|
|
|
|
2024-08-04 15:27:29 -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
|
|
|
|
}
|
2024-08-04 15:27:29 -07:00
|
|
|
defer privWriter.Close()
|
2024-08-04 14:34:41 -07:00
|
|
|
|
2024-08-04 15:27:29 -07:00
|
|
|
err = ent.SerializePrivate(privWriter, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-04 14:34:41 -07:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|