title: Equivalent of gpg --sign in golang?
body: <p>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.</p>
  
  <p>The relevant line in Perl:</p>
  
  <pre><code>my $token = `printf $emailaddress | gpg --sign -u myprivatekeyname | base64`;
  </code></pre>
  
  <p>My attempt in Go:</p>
  
  <pre><code>import (
      "golang.org/x/crypto/openpgp"
      "log"
      "fmt"
      "strings"
      "bytes"
      )
  
  
  const privateKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
  &lt;the same private key as is used by the GPG tool&gt;
  -----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 &amp;&amp; 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 &amp;&amp; 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())
  }
  </code></pre>
  
  <p>Unfortunately the results for a given email addresss (like 'bob@foo.com') are not identical. </p>
  
  <p>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).</p>
  
  <p>Any idea what Iam doing wrong?</p>