Skip to content

Instantly share code, notes, and snippets.

@rgarcia
Last active December 22, 2015 20:59
Show Gist options
  • Select an option

  • Save rgarcia/6530048 to your computer and use it in GitHub Desktop.

Select an option

Save rgarcia/6530048 to your computer and use it in GitHub Desktop.
pgp decrypt in go
package main
import (
gopgp "code.google.com/p/go.crypto/openpgp"
gopgperrors "code.google.com/p/go.crypto/openpgp/errors"
"fmt"
"io"
"os"
)
// Really stupid gopgp.KeyRing implementation
type PGPKeyring struct {
keys []gopgp.Key
}
func (keyring PGPKeyring) KeysById(id uint64) []gopgp.Key {
return keyring.keys
}
func (keyring PGPKeyring) DecryptionKeys() []gopgp.Key {
return keyring.keys
}
func main() {
// load the private key
keyFile := "pgp_privkey"
file, err := os.Open(keyFile)
if err != nil {
panic(err)
}
defer file.Close()
el, err := gopgp.ReadKeyRing(file)
if err != nil {
panic(err)
}
keyring := PGPKeyring{el.DecryptionKeys()}
prompt := func(keys []gopgp.Key, symmetric bool) ([]byte, error) {
err := keys[0].PrivateKey.Decrypt([]byte("passcode"))
if err != nil {
return nil, gopgperrors.ErrKeyIncorrect
}
return nil, nil
}
// decrypt a file
encryptedFilename := "encryptedfile.pgp"
encryptedFile, err := os.Open(encryptedFilename)
if err != nil {
panic(err)
}
defer encryptedFile.Close()
md, err := gopgp.ReadMessage(encryptedFile, keyring, prompt, nil)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, md.UnverifiedBody)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment