|
1
|
|
|
package kms |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/base64" |
|
6
|
|
|
) |
|
7
|
|
|
|
|
8
|
|
|
func (svc *KMS) DeleteKey(ctx context.Context, key string, day ...int64) error { |
|
|
|
|
|
|
9
|
|
|
metaData, err := svc.DescribeKey(ctx, DescribeKeyRequest{ |
|
10
|
|
|
KeyID: key, |
|
11
|
|
|
}) |
|
12
|
|
|
if err != nil { |
|
13
|
|
|
return err |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
const defaultWindowDay = 30 |
|
17
|
|
|
d := int64(defaultWindowDay) |
|
18
|
|
|
if len(day) != 0 { |
|
19
|
|
|
d = day[0] |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
_, err = svc.ScheduleKeyDeletion(ctx, ScheduleKeyDeletionRequest{ |
|
23
|
|
|
KeyID: metaData.KeyMetadata.KeyID, |
|
24
|
|
|
PendingWindowInDays: d, |
|
25
|
|
|
}) |
|
26
|
|
|
return err |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
func (svc *KMS) EncryptBytes(ctx context.Context, key string, plainData []byte) (encryptedData []byte, err error) { |
|
|
|
|
|
|
30
|
|
|
result, err := svc.Encrypt(ctx, EncryptRequest{ |
|
31
|
|
|
KeyID: key, |
|
32
|
|
|
Plaintext: plainData, |
|
33
|
|
|
}) |
|
34
|
|
|
if err != nil { |
|
35
|
|
|
return nil, err |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return result.CiphertextBlob, nil |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
func (svc *KMS) EncryptString(ctx context.Context, key string, plainText string) (base64Text string, err error) { |
|
|
|
|
|
|
42
|
|
|
encryptedData, err := svc.EncryptBytes(ctx, key, []byte(plainText)) |
|
43
|
|
|
if err != nil { |
|
44
|
|
|
return "", err |
|
45
|
|
|
} |
|
46
|
|
|
return base64.StdEncoding.EncodeToString(encryptedData), nil |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
func (svc *KMS) DecryptBytes(ctx context.Context, encryptedData []byte) (plainData []byte, err error) { |
|
|
|
|
|
|
50
|
|
|
result, err := svc.Decrypt(ctx, DecryptRequest{ |
|
51
|
|
|
CiphertextBlob: encryptedData, |
|
52
|
|
|
}) |
|
53
|
|
|
if err != nil { |
|
54
|
|
|
return nil, err |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return result.Plaintext, nil |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
func (svc *KMS) DecryptString(ctx context.Context, base64Text string) (plainText string, err error) { |
|
|
|
|
|
|
61
|
|
|
byt, err := base64.StdEncoding.DecodeString(base64Text) |
|
62
|
|
|
if err != nil { |
|
63
|
|
|
return "", err |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
plainData, err := svc.DecryptBytes(ctx, byt) |
|
67
|
|
|
if err != nil { |
|
68
|
|
|
return "", err |
|
69
|
|
|
} |
|
70
|
|
|
return string(plainData), nil |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
func (svc *KMS) ReEncryptBytes(ctx context.Context, destinationKey string, encryptedData []byte) (resultEncryptedData []byte, err error) { |
|
|
|
|
|
|
74
|
|
|
result, err := svc.ReEncrypt(ctx, ReEncryptRequest{ |
|
75
|
|
|
DestinationKeyID: destinationKey, |
|
76
|
|
|
CiphertextBlob: encryptedData, |
|
77
|
|
|
}) |
|
78
|
|
|
if err != nil { |
|
79
|
|
|
return nil, err |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return result.CiphertextBlob, nil |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
func (svc *KMS) ReEncryptString(ctx context.Context, destinationKey, base64Text string) (resultBase64Text string, err error) { |
|
|
|
|
|
|
86
|
|
|
byt, err := base64.StdEncoding.DecodeString(base64Text) |
|
87
|
|
|
if err != nil { |
|
88
|
|
|
return "", err |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
encryptedData, err := svc.ReEncryptBytes(ctx, destinationKey, byt) |
|
92
|
|
|
if err != nil { |
|
93
|
|
|
return "", err |
|
94
|
|
|
} |
|
95
|
|
|
return base64.StdEncoding.EncodeToString(encryptedData), nil |
|
96
|
|
|
} |
|
97
|
|
|
|