-
Notifications
You must be signed in to change notification settings - Fork 2
/
kms.go
62 lines (42 loc) · 1.05 KB
/
kms.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
package envsec
import (
"encoding/base64"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
)
type KMSMethod struct {
client *kms.KMS
keyId string
}
func NewKMSMethod(region, keyId string) (*KMSMethod, error) {
method := &KMSMethod{
client: kms.New(session.New(), &aws.Config{Region: aws.String(region)}),
keyId: keyId,
}
return method, nil
}
func (c *KMSMethod) Decrypt(ciphertext string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return empty, err
}
resp, err := c.client.Decrypt(&kms.DecryptInput{
CiphertextBlob: decoded,
})
if err != nil {
return empty, err
}
return string(resp.Plaintext), nil
}
func (c *KMSMethod) Encrypt(plaintext string) (string, error) {
resp, err := c.client.Encrypt(&kms.EncryptInput{
Plaintext: []byte(plaintext),
KeyId: aws.String(c.keyId),
})
if err != nil {
return empty, err
}
encoded := base64.StdEncoding.EncodeToString(resp.CiphertextBlob)
return encoded, nil
}