aboutsummaryrefslogtreecommitdiffstats
path: root/build/utils.go
blob: df869efd0c7c7d6f1066533680a74b2d817094d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// +build none

package build

import (
    "bytes"
    "encoding/base64"

    "golang.org/x/crypto/openpgp"
    "golang.org/x/crypto/openpgp/armor"
)

// GenerateEncodedPGPKeyPair generate base64 encoded PGP key pair
func GenerateEncodedPGPKeyPair(name, comment, email string) (string, error) {
    var e *openpgp.Entity
    e, err := openpgp.NewEntity(name, comment, email, nil)
    if err != nil {
        return "", err
    }

    keyBuffer := bytes.Buffer{}
    w, err := armor.Encode(&keyBuffer, openpgp.PrivateKeyType, nil)
    if err != nil {
        return "", err
    }

    err = e.SerializePrivate(w, nil)
    if err != nil {
        return "", err
    }
    w.Close()

    base64Buffer := bytes.Buffer{}
    en := base64.NewEncoder(base64.StdEncoding, &base64Buffer)
    en.Write(keyBuffer.Bytes())
    en.Close()

    return base64Buffer.String(), nil
}