Debian Codegraph @

lib/crypto/gpg/gpgbin/encrypt.go

raw
1 package gpgbin
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7
8 "git.sr.ht/~rjarry/aerc/models"
9 )
10
11 // Encrypt runs gpg --encrypt [--sign] -r [recipient]
12 func Encrypt(r io.Reader, to []string, from string) ([]byte, error) {
13 args := []string{
14 "--armor",
15 }
16 if from != "" {
17 args = append(args, "--sign", "--default-key", from)
18 }
19 for _, rcpt := range to {
20 args = append(args, "--recipient", rcpt)
21 }
22 args = append(args, "--encrypt", "-")
23
24 g := newGpg(r, args)
25 _ = g.cmd.Run()
26 var md models.MessageDetails
27 err := parseStatusFd(bytes.NewReader(g.stderr.Bytes()), &md)
28 if err != nil {
29 return nil, fmt.Errorf("gpg: failure to encrypt: %w. check public key(s)", err)
30 }
31
32 return g.stdout.Bytes(), nil
33 }