aboutsummaryrefslogtreecommitdiff
path: root/gpg/gpg.go
blob: 1ab71eccbdde1d6176bfd89b85bc594c7c332364 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package gpg

// Package gpg wraps around the gpg command line tool and exposes some of its functionality

import (
	"bytes"
	"os/exec"
)

// Signer implements objects.Signer using gpg
type Signer struct {
	Key string
}

func filter(cmd *exec.Cmd, b []byte) ([]byte, error) {
	cmd.Stdin = bytes.NewReader(b)
	var out bytes.Buffer
	cmd.Stdout = &out

	err := cmd.Run()
	return out.Bytes(), err
}

// Sign signs a message b with the key s.Key
func (s Signer) Sign(b []byte) ([]byte, error) {
	cmd := exec.Command("gpg", "--clearsign", "-u", s.Key)
	return filter(cmd, b)
}

// Verifyer implements objects.Verifyer using gpg
type Verifyer struct{}

// Verify verifies the signed message b
func (Verifyer) Verify(b []byte) error {
	cmd := exec.Command("gpg", "--verify")
	cmd.Stdin = bytes.NewReader(b)
	return cmd.Run()
}

type Encrypter struct {
	Key string
}

func (e Encrypter) Encrypt(b []byte) ([]byte, error) {
	cmd := exec.Command("gpg", "--encrypt", "--recipient", e.Key)
	return filter(cmd, b)
}

type Decrypter struct{}

func (Decrypter) Decrypt(b []byte) ([]byte, error) {
	cmd := exec.Command("gpg", "--decrypt")
	return filter(cmd, b)
}

type Crypter struct {
	Encrypter
	Decrypter
}