-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.go
58 lines (48 loc) · 1.41 KB
/
decrypt.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
package kmsdecrypt
import (
"encoding/base64"
"github.com/aws/aws-sdk-go/service/kms"
)
// decryptKeyValue is ment to be run in a go-routine as sends the key, value and possibly any errors to the res channel.
func (d *KmsDecrypter) decryptKeyValue(key *string, val *string, res chan<- resultKeyValue) {
decrypted, err := d.kmsDecrypt(*val)
result := resultKeyValue{
key: *key,
val: decrypted,
}
// If we had any errors add the error to the result.
if err != nil {
result.err = err
}
// Send the result to the result channel.
res <- result
}
// decryptString is ment to be run in a go-routine as sends the str and possibly any errors to the res channel.
func (d *KmsDecrypter) decryptString(str *string, res chan<- resultString) {
decrypted, err := d.kmsDecrypt(*str)
result := resultString{
str: decrypted,
}
// If we had any errors add the error to the result.
if err != nil {
result.err = err
}
// Send the result to the result channel.
res <- result
}
// kmsdecrypt uses aws kms to decrypt the value
func (d *KmsDecrypter) kmsDecrypt(val string) (string, error) {
svc := kms.New(d.session)
// Decode from base64 to []byte.
decoded, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return "", err
}
// Decrypt using KMS.
params := &kms.DecryptInput{CiphertextBlob: decoded}
resp, err := svc.Decrypt(params)
if err != nil {
return "", err
}
return string(resp.Plaintext), nil
}