Debian Codegraph @

lib/crypto/gpg/gpgbin/keys.go

raw
1 package gpgbin
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "os/exec"
8 "strings"
9 )
10
11 // GetPrivateKeyId runs gpg --list-secret-keys s
12 func GetPrivateKeyId(s string) (string, error) {
13 private := true
14 id := getKeyId(s, private)
15 if id == "" {
16 return "", fmt.Errorf("no private key found")
17 }
18 return id, nil
19 }
20
21 // GetKeyId runs gpg --list-keys s
22 func GetKeyId(s string) (string, error) {
23 private := false
24 id := getKeyId(s, private)
25 if id == "" {
26 return "", fmt.Errorf("no public key found")
27 }
28 return id, nil
29 }
30
31 // ExportPublicKey exports the public key identified by k in armor format
32 func ExportPublicKey(k string) (io.Reader, error) {
33 cmd := exec.Command("gpg", "--armor",
34 "--export-options", "export-minimal", "--export", k)
35
36 var outbuf bytes.Buffer
37 var stderr strings.Builder
38 cmd.Stdout = &outbuf
39 cmd.Stderr = &stderr
40 err := cmd.Run()
41 if err != nil {
42 return nil, fmt.Errorf("gpg: export failed: %w", err)
43 }
44 if strings.Contains(stderr.String(), "gpg") {
45 return nil, fmt.Errorf("gpg: error exporting key")
46 }
47 return &outbuf, nil
48 }