-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdecryptor.go
61 lines (48 loc) · 1022 Bytes
/
decryptor.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
package keygen
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/base64"
"strings"
)
type decryptor struct {
Secret string
}
func (d *decryptor) DecryptCertificate(cert *certificate) ([]byte, error) {
parts := strings.SplitN(cert.Enc, ".", 3)
// Decode parts
ciphertext, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
return nil, err
}
iv, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
tag, err := base64.StdEncoding.DecodeString(parts[2])
if err != nil {
return nil, err
}
// Hash secret
h := sha256.New()
h.Write([]byte(d.Secret))
key := h.Sum(nil)
// Setup AES
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aes, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
// Append auth tag to ciphertext
ciphertext = append(ciphertext, tag...)
// Decrypt
plaintext, err := aes.Open(nil, iv, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}