diff options
Diffstat (limited to 'lib/crypto/gpg/gpgbin/decrypt.go')
-rw-r--r-- | lib/crypto/gpg/gpgbin/decrypt.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/crypto/gpg/gpgbin/decrypt.go b/lib/crypto/gpg/gpgbin/decrypt.go new file mode 100644 index 00000000..4b8d8f2b --- /dev/null +++ b/lib/crypto/gpg/gpgbin/decrypt.go @@ -0,0 +1,34 @@ +package gpgbin + +import ( + "bytes" + "io" + "io/ioutil" + + "git.sr.ht/~rjarry/aerc/models" +) + +// Decrypt runs gpg --decrypt on the contents of r. If the packet is signed, +// the signature is also verified +func Decrypt(r io.Reader) (*models.MessageDetails, error) { + md := new(models.MessageDetails) + orig, err := ioutil.ReadAll(r) + if err != nil { + return md, err + } + args := []string{"--decrypt"} + g := newGpg(bytes.NewReader(orig), args) + err = g.cmd.Run() + if err != nil { + err = parseError(g.stderr.String()) + switch GPGErrors[err.Error()] { + case ERROR_NO_PGP_DATA_FOUND: + md.Body = bytes.NewReader(orig) + return md, nil + default: + } + } + outRdr := bytes.NewReader(g.stdout.Bytes()) + parse(outRdr, md) + return md, nil +} |