Total Lines | 68 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package kms |
||
2 | |||
3 | import ( |
||
4 | "context" |
||
5 | |||
6 | SDK "github.com/aws/aws-sdk-go-v2/service/kms" |
||
7 | |||
8 | "github.com/evalphobia/aws-sdk-go-v2-wrapper/errors" |
||
9 | "github.com/evalphobia/aws-sdk-go-v2-wrapper/private/pointers" |
||
10 | ) |
||
11 | |||
12 | // Encrypt executes `Encrypt` operation. |
||
13 | func (svc *KMS) Encrypt(ctx context.Context, r EncryptRequest) (*EncryptResult, error) { |
||
14 | out, err := svc.RawEncrypt(ctx, r.ToInput()) |
||
15 | if err != nil { |
||
16 | err = svc.errWrap(errors.ErrorData{ |
||
17 | Err: err, |
||
18 | AWSOperation: "Encrypt", |
||
19 | }) |
||
20 | svc.Errorf(err.Error()) |
||
|
|||
21 | return nil, err |
||
22 | } |
||
23 | return NewEncryptResult(out), nil |
||
24 | } |
||
25 | |||
26 | // EncryptRequest has parameters for `Encrypt` operation. |
||
27 | type EncryptRequest struct { |
||
28 | KeyID string |
||
29 | Plaintext []byte |
||
30 | |||
31 | // optional |
||
32 | EncryptionAlgorithm EncryptionAlgorithmSpec |
||
33 | EncryptionContext map[string]string |
||
34 | GrantTokens []string |
||
35 | } |
||
36 | |||
37 | func (r EncryptRequest) ToInput() *SDK.EncryptInput { |
||
38 | in := &SDK.EncryptInput{} |
||
39 | |||
40 | if r.KeyID != "" { |
||
41 | in.KeyId = pointers.String(r.KeyID) |
||
42 | } |
||
43 | in.Plaintext = r.Plaintext |
||
44 | |||
45 | in.EncryptionAlgorithm = SDK.EncryptionAlgorithmSpec(r.EncryptionAlgorithm) |
||
46 | in.EncryptionContext = r.EncryptionContext |
||
47 | in.GrantTokens = r.GrantTokens |
||
48 | return in |
||
49 | } |
||
50 | |||
51 | type EncryptResult struct { |
||
52 | CiphertextBlob []byte |
||
53 | EncryptionAlgorithm EncryptionAlgorithmSpec |
||
54 | KeyID string |
||
55 | } |
||
56 | |||
57 | func NewEncryptResult(output *SDK.EncryptResponse) *EncryptResult { |
||
58 | r := &EncryptResult{} |
||
59 | if output == nil { |
||
60 | return r |
||
61 | } |
||
62 | |||
63 | r.CiphertextBlob = output.CiphertextBlob |
||
64 | r.EncryptionAlgorithm = EncryptionAlgorithmSpec(output.EncryptionAlgorithm) |
||
65 | if output.KeyId != nil { |
||
66 | r.KeyID = *output.KeyId |
||
67 | } |
||
68 | return r |
||
69 | } |
||
70 |