Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 1ecb988

Browse files
committed
plumbing: object, add Commit.Verify method
Commit.Verify() performs PGP verification of a signed commit given an armored keyring.
1 parent b08cc8d commit 1ecb988

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

plumbing/object/commit.go

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99
"strings"
1010

11+
"golang.org/x/crypto/openpgp"
12+
1113
"gopkg.in/src-d/go-git.v4/plumbing"
1214
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1315
"gopkg.in/src-d/go-git.v4/utils/ioutil"
@@ -311,6 +313,34 @@ func (c *Commit) String() string {
311313
)
312314
}
313315

316+
// Verify performs PGP verification of the commit with a provided armored
317+
// keyring and returns openpgp.Entity associated with verifying key on success.
318+
func (c *Commit) Verify(armoredKeyRing string) (*openpgp.Entity, error) {
319+
keyRingReader := strings.NewReader(armoredKeyRing)
320+
keyring, err := openpgp.ReadArmoredKeyRing(keyRingReader)
321+
if err != nil {
322+
return nil, err
323+
}
324+
325+
// Extract signature.
326+
signature := strings.NewReader(c.PGPSignature)
327+
328+
// Remove signature. Keep only the commit components.
329+
c.PGPSignature = ""
330+
331+
// Encode commit and get a reader object.
332+
encoded := &plumbing.MemoryObject{}
333+
if err := c.Encode(encoded); err != nil {
334+
return nil, err
335+
}
336+
er, err := encoded.Reader()
337+
if err != nil {
338+
return nil, err
339+
}
340+
341+
return openpgp.CheckArmoredDetachedSignature(keyring, er, signature)
342+
}
343+
314344
func indent(t string) string {
315345
var output []string
316346
for _, line := range strings.Split(t, "\n") {

0 commit comments

Comments
 (0)