forked from gruntwork-io/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpg.go
42 lines (35 loc) · 1.08 KB
/
gpg.go
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
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"golang.org/x/crypto/openpgp"
"os"
)
func gpgVerify(gpgPubKey string, gpgSigPath string, assetPath string) error {
pubKeyReader, err := os.Open(gpgPubKey)
defer pubKeyReader.Close()
if err != nil {
return fmt.Errorf("Failed to open %s for reading: %s", gpgPubKey, err)
}
sigReader, err := os.Open(gpgSigPath)
defer sigReader.Close()
if err != nil {
return fmt.Errorf("Failed to open %s for reading: %s", gpgSigPath, err)
}
assetReader, err := os.Open(assetPath)
defer assetReader.Close()
if err != nil {
return fmt.Errorf("Failed to open %s for reading: %s", assetPath, err)
}
keyring, err := openpgp.ReadArmoredKeyRing(pubKeyReader)
if err != nil {
return fmt.Errorf("Failed to create armored key ring (pub key: %s): %s", gpgPubKey, err)
}
gpg, err := openpgp.CheckArmoredDetachedSignature(keyring, assetReader, sigReader)
if err != nil {
return fmt.Errorf("GPG check failed (asset: %s, pub key: %s): %s", assetPath, gpgPubKey, err)
}
for id := range gpg.Identities {
fmt.Printf("GPG ID for asset %s: %s\n", assetPath, id)
}
return nil
}