2022-04-25 13:30:44 +00:00
|
|
|
package gpgbin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-07-01 17:11:33 +00:00
|
|
|
"fmt"
|
2022-04-25 13:30:44 +00:00
|
|
|
"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)
|
2022-07-29 20:31:54 +00:00
|
|
|
err := g.cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("failed to run signing: %w", err)
|
|
|
|
}
|
2022-04-25 13:30:44 +00:00
|
|
|
|
|
|
|
outRdr := bytes.NewReader(g.stdout.Bytes())
|
|
|
|
var md models.MessageDetails
|
2022-07-29 20:31:54 +00:00
|
|
|
err = parse(outRdr, &md)
|
2022-07-01 17:11:33 +00:00
|
|
|
if err != nil {
|
2022-07-31 13:15:27 +00:00
|
|
|
return nil, "", fmt.Errorf("failed to parse messagedetails: %w", err)
|
2022-07-01 17:11:33 +00:00
|
|
|
}
|
2022-04-25 13:30:44 +00:00
|
|
|
var buf bytes.Buffer
|
2022-07-29 20:31:54 +00:00
|
|
|
_, _ = io.Copy(&buf, md.Body)
|
2022-04-25 13:30:44 +00:00
|
|
|
return buf.Bytes(), md.Micalg, nil
|
|
|
|
}
|