/
signer.go
78 lines (61 loc) · 1.46 KB
/
signer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package signer
import (
"bytes"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/clearsign"
"log"
"os"
"path/filepath"
)
type Signer struct {
Email string
Privring openpgp.EntityList
Entity *openpgp.Entity
Path string
}
func (s *Signer) GetKeyByEmail(keyring openpgp.EntityList, email string) *openpgp.Entity {
for _, entity := range keyring {
for _, ident := range entity.Identities {
if ident.UserId.Email == email {
return entity
}
}
}
return nil
}
// it will sign the sha256 hash (or any string) and save it
// under the path (signer Path) and uuid as file name
func (s *Signer) SignIt(sha256 string, uuid string) {
var buf bytes.Buffer
w, err := clearsign.Encode(&buf, s.Entity.PrivateKey, nil)
if err != nil {
log.Fatal(err)
}
_, _ = w.Write([]byte(sha256))
w.Close()
ret := buf.Bytes()
f, err := os.Create(filepath.Join(s.Path, uuid))
defer f.Close()
if err != nil {
log.Fatal(err)
}
f.WriteString(string(ret[:]))
}
// privring_path is where you exported your private ring
// with gpg --export-secret-keys > privring.gpg
func NewSigner(email string, privring_path string, signdir_path string) Signer {
signer := Signer{
Email: email,
Path: signdir_path,
}
privRingKeyFile, err := os.Open(privring_path)
if err != nil {
log.Fatal(err)
}
privring, err := openpgp.ReadKeyRing(privRingKeyFile)
if err != nil {
log.Fatal(err)
}
signer.Entity = signer.GetKeyByEmail(privring, email)
return signer
}