aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto/gpg/gpgbin/sign.go
blob: caf31d1436bdb20c67f7e551d7f9324adda9ebde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package gpgbin

import (
	"bytes"
	"fmt"
	"io"

	"git.sr.ht/~rjarry/aerc/models"
)

// Sign creates a detached signature based on the contents of r
func Sign(r io.Reader, from string) ([]byte, string, error) {
	args := []string{
		"--armor",
		"--detach-sign",
		"--default-key", from,
	}

	g := newGpg(r, args)
	err := g.cmd.Run()
	if err != nil {
		return nil, "", fmt.Errorf("failed to run signing: %w", err)
	}

	outRdr := bytes.NewReader(g.stdout.Bytes())
	var md models.MessageDetails
	err = parse(outRdr, &md)
	if err != nil {
		return nil, "", fmt.Errorf("failed to parse messagedetails: %v", err)
	}
	var buf bytes.Buffer
	_, _ = io.Copy(&buf, md.Body)
	return buf.Bytes(), md.Micalg, nil
}