forked from oleiade/trousseau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decryption.go
75 lines (61 loc) · 1.26 KB
/
decryption.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
package trousseau
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
_ "crypto/ecdsa"
_ "crypto/sha1"
_ "crypto/sha256"
_ "crypto/sha512"
"code.google.com/p/go.crypto/openpgp"
"code.google.com/p/go.crypto/openpgp/armor"
)
var (
password string
keys openpgp.EntityList
)
func initCrypto(keyRingPath, pass string) {
f, err := os.Open(keyRingPath)
if err != nil {
log.Fatalf("Can't open keyring: %v", err)
}
defer f.Close()
keys, err = openpgp.ReadKeyRing(f)
if err != nil {
log.Fatalf("Can't read keyring: %v", err)
}
password = pass
}
func decrypt(s string) (string, error) {
if s == "" {
return "", nil
}
raw, err := armor.Decode(strings.NewReader(s))
if err != nil {
return "", err
}
d, err := openpgp.ReadMessage(raw.Body, keys,
func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
kp := []byte(password)
if symmetric {
return kp, nil
}
for _, k := range keys {
err := k.PrivateKey.Decrypt(kp)
if err == nil {
return nil, nil
}
}
return nil, fmt.Errorf("Whether no valid private key for" +
"store decryption was available or " +
"supplied password was invalid")
},
nil)
if err != nil {
return "", err
}
bytes, err := ioutil.ReadAll(d.UnverifiedBody)
return string(bytes), err
}