-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdecoder.go
53 lines (45 loc) · 1.26 KB
/
decoder.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
package gpg
import (
"bytes"
"errors"
"golang.org/x/crypto/openpgp"
"io"
)
func Decode(key, passphrase []byte, src io.Reader, dest io.Writer) error {
dec := &Decoder{key, passphrase}
return dec.Decode(src, dest)
}
type Decoder struct {
Key []byte
Passphrase []byte
}
func (d *Decoder) Decode(r io.Reader, w io.Writer) error {
entitylist, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(d.Key))
if err != nil {
return err
}
entity := entitylist[0]
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
if len(d.Passphrase) == 0 {
return errors.New("Private key is encrypted but you did not provide a passphrase")
}
err := entity.PrivateKey.Decrypt(d.Passphrase)
if err != nil {
return errors.New("Failed to decrypt private key. Did you use the wrong passphrase? (" + err.Error() + ")")
}
}
for _, subkey := range entity.Subkeys {
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
err := subkey.PrivateKey.Decrypt(d.Passphrase)
if err != nil {
return errors.New("Failed to decrypt subkey. Did you use the wrong passphrase? (" + err.Error() + ")")
}
}
}
read, err := openpgp.ReadMessage(r, entitylist, nil, nil)
if err != nil {
return err
}
_, err = io.Copy(w, read.LiteralData.Body)
return err
}