forked from awsdocs/aws-doc-sdk-examples
/
ReEncryptData.go
73 lines (62 loc) · 2.24 KB
/
ReEncryptData.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[kms.go.reencrypt_data]
package main
// snippet-start:[kms.go.reencrypt_data.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
)
// snippet-end:[kms.go.reencrypt_data.imports]
// ReEncryptText reencrypts some text using a new AWS Key Management Service (AWS KMS) key (KMS key).
// Inputs:
// svc is an AWS KMS service client
// keyID is the ID of a different KMS key
// Output:
// If success, information about the reencrypted text and nil
// Otherwise, nil and an error from the call to ReEncrypt
func ReEncryptText(svc kmsiface.KMSAPI, blob *[]byte, keyID *string) (*kms.ReEncryptOutput, error) {
// snippet-start:[kms.go.reencrypt_data.call]
result, err := svc.ReEncrypt(&kms.ReEncryptInput{
CiphertextBlob: *blob,
DestinationKeyId: keyID,
})
// snippet-end:[kms.go.reencrypt_data.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// snippet-start:[kms.go.reencrypt_data.args]
keyID := flag.String("k", "", "The ID of a KMS key")
data := flag.String("d", "", "The data to reencrypt, as a string")
flag.Parse()
if *keyID == "" || *data == "" {
fmt.Println("You must supply the ID of a KMS key and data")
fmt.Println("-k KEY-ID -d DATA")
return
}
// snippet-end:[kms.go.reencrypt_data.args]
// snippet-start:[kms.go.reencrypt_data.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := kms.New(sess)
blob := []byte(*data)
// snippet-end:[kms.go.reencrypt_data.session]
result, err := ReEncryptText(svc, &blob, keyID)
if err != nil {
fmt.Println("Got error reencrypting data:")
fmt.Println(err)
return
}
// snippet-start:[kms.go.reencrypt_data.display]
fmt.Println("Blob (base-64 byte array):")
fmt.Println(result.CiphertextBlob)
// snippet-end:[kms.go.reencrypt_data.display]
}
// snippet-end:[kms.go.reencrypt_data]