99 lines
1.6 KiB
Go
99 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
//"crypto/rand"
|
|
//"crypto/rsa"
|
|
//"crypto/x509"
|
|
//"encoding/pem"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
//"bytes"
|
|
//"io"
|
|
|
|
"github.com/ProtonMail/go-crypto/openpgp"
|
|
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
|
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
|
)
|
|
|
|
func main() {
|
|
err := run()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
type Ident struct {
|
|
Name, Comment, Email string
|
|
}
|
|
|
|
func run() error {
|
|
idents := []Ident{
|
|
{"Company", "", "main@company.com"},
|
|
{"Customer", "", "customer@example.com"},
|
|
}
|
|
|
|
for _, ident := range idents {
|
|
fmt.Println("Generating keypair for", ident.Name)
|
|
err := keypair(ident)
|
|
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,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Public
|
|
pubOut, err := os.Create(filepath.Join(keyDir, "public", ident.Name+".asc"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pubOut.Close()
|
|
|
|
pubWriter, err := armor.Encode(pubOut, "PGP PUBLIC KEY BLOCK", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pubWriter.Close()
|
|
|
|
err = ent.Serialize(pubWriter)
|
|
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()
|
|
|
|
privWriter, err := armor.Encode(privOut, "PGP PRIVATE KEY BLOCK", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer privWriter.Close()
|
|
|
|
err = ent.SerializePrivate(privWriter, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|