Go is currently my favourite programming language for recreational programming. Mentioning Go appears to send some people into a rage judging by the sort of comments that arise on Hacker News, proggit and so on but, thankfully, this blog doesn't have comments.

Over the past few months I've been (very) slowly building OpenPGP support into Go's crypto library. (You can get my key, DNSSEC signed, via PKA: dig +dnssec -t txt agl._pka.imperialviolet.org ). I don't get to use PGP often enough but, even when I have (I used to run an email verifying auto-signer), I've resorted to shelling out to gpg . I understand that GPG's support for acting as a library has improved considerably since then, but half the aim of this is that it's recreational programming.

So, here's a complete Go program using the OpenPGP library: (Note, if you wish to compile these programs you'll need a tip-of-tree Go)

package main import ( "crypto/openpgp" "crypto/openpgp/armor" "fmt" "os" ) func main() { w, _ := armor.Encode(os.Stdout, "PGP MESSAGE", nil) plaintext, _ := openpgp.SymmetricallyEncrypt(w, []byte("golang"), nil) fmt.Fprintf(plaintext, "Hello from golang.

") plaintext.Close() w.Close() fmt.Print("

") }

It'll output a symmetrically encrypted message, like this:

-----BEGIN PGP MESSAGE----- wx4EBwMCkdZyiLAEewZgIuDNFqo0FBYNp4ZGpaiaAjHS4AHkLcerCkW9sCqLdBQc GH6HUOEwZeCr4ILhUBHgnOID5zAh4PDkdVSUDxFj0KITDfgDXMptL+Ai4aTL4Mzg 4uCX5C7gKEnS8gsxdwC67zQzUMbiSS92duG39AA= =NuOw -----END PGP MESSAGE-----

You can feed that into gpg -c and decrypt with the passphrase golang if you like. Since all the APIs are stream based you can process arbitrary length messages without having to fit them into memory.

We can also do public key stuff:

package main import ( "crypto/openpgp" "crypto/openpgp/armor" "fmt" "os" ) func getKeyByEmail(keyring openpgp.EntityList, email string) *openpgp.Entity { for _, entity := range keyring { for _, ident := range entity.Identities { if ident.UserId.Email == email { return entity } } } return nil } func main() { pubringFile, _ := os.Open("pubring.gpg") pubring, _ := openpgp.ReadKeyRing(pubringFile) privringFile, _ := os.Open("secring.gpg") privring, _ := openpgp.ReadKeyRing(privringFile) myPrivateKey := getKeyByEmail(privring, "me@mydomain.com") theirPublicKey := getKeyByEmail(pubring, "bob@example.com") w, _ := armor.Encode(os.Stdout, "PGP MESSAGE", nil) plaintext, _ := openpgp.Encrypt(w, []*openpgp.Entity{theirPublicKey}, myPrivateKey, nil) fmt.Fprintf(plaintext, "Hello from golang.

") plaintext.Close() w.Close() fmt.Printf("

") }

And, of course, it can also decrypt those messages, check signatures etc. The missing bits are ElGamal encryption, support for clearsigning and big bits of functionality around web-of-trust, manipulating keys etc. None the less, Camlistore is already using it and it appears to be working for them.