I currently have a Perl script that performs a PGP sign on an email address to produce an obfuscated token. The Perl script uses the command line utility GPG version 2.2.8 (on MacOS). I would like to convert this functionality to Go.
The relevant line in Perl:
my $token = `printf $emailaddress | gpg --sign -u myprivatekeyname | base64`;
My attempt in Go:
import (
"golang.org/x/crypto/openpgp"
"log"
"fmt"
"strings"
"bytes"
)
const privateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
<the same private key as is used by the GPG tool>
-----END PGP PRIVATE KEY BLOCK-----`
func main() {
email := "bob@foo.com"
token := makeToken(email)
fmt.Println(token)
}
func getPrivateKey() *openpgp.Entity {
pp := "mypassphrase"
ppb := []byte(pp)
entitylist, err := openpgp.ReadArmoredKeyRing(strings.NewReader(privateKey))
if err != nil {
log.Fatal(err)
}
entity := entitylist[0]
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
err := entity.PrivateKey.Decrypt(ppb)
if err != nil {
fmt.Println("Failed to decrypt key")
}
}
for _, subkey := range entity.Subkeys {
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
err := subkey.PrivateKey.Decrypt(ppb)
if err != nil {
fmt.Println("Failed to decrypt subkey")
}
}
}
return entity
}
func makeToken(email string) string {
pk := getPrivateKey()
out := new(bytes.Buffer)
in, err := openpgp.Sign(out, pk, nil, nil)
if err != nil {
log.Fatal(err)
}
in.Write([]byte(email))
in.Close()
return base64.StdEncoding.EncodeToString(out.Bytes())
}
Unfortunately the results for a given email addresss (like 'bob@foo.com') are not identical.
I am using the same private key: I exported it from GPG and pasted it into my go code (obviously this is just for testing, I won't leave it like that).
Any idea what Iam doing wrong?