-
Notifications
You must be signed in to change notification settings - Fork 0
/
awskms.go
79 lines (63 loc) · 1.67 KB
/
awskms.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
76
77
78
79
package awskms
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
)
var (
// Client contains AWS KMS client
Client kmsiface.KMSAPI
// KeyID contains the master key
KeyID string
)
// Encrypt performs encryption on plaintext with AWS KMS
// Returns empty []byte on error
func Encrypt(plaintext []byte) ([]byte, error) {
input := &kms.EncryptInput{
KeyId: aws.String(KeyID),
Plaintext: plaintext,
}
res, err := Client.Encrypt(input)
if err != nil {
return []byte{}, err
}
return res.CiphertextBlob, nil
}
// EncryptWithContext performs encryption on plaintext with AWS KMS
// Returns empty []byte on error
func EncryptWithContext(ctx context.Context, plaintext []byte) ([]byte, error) {
input := &kms.EncryptInput{
KeyId: aws.String(KeyID),
Plaintext: plaintext,
}
res, err := Client.EncryptWithContext(ctx, input)
if err != nil {
return []byte{}, err
}
return res.CiphertextBlob, nil
}
// Decrypt performs decryption on ciphertext with AWS KMS
// Returns empty []byte on error
func Decrypt(ciphertext []byte) ([]byte, error) {
input := &kms.DecryptInput{
CiphertextBlob: ciphertext,
}
res, err := Client.Decrypt(input)
if err != nil {
return []byte{}, err
}
return res.Plaintext, nil
}
// DecryptWithContext performs decryption on ciphertext with AWS KMS
// Returns empty []byte on error
func DecryptWithContext(ctx context.Context, ciphertext []byte) ([]byte, error) {
input := &kms.DecryptInput{
CiphertextBlob: ciphertext,
}
res, err := Client.DecryptWithContext(ctx, input)
if err != nil {
return []byte{}, err
}
return res.Plaintext, nil
}