Created
January 8, 2019 19:15
-
-
Save moosh3/49824ae3d82531fe5c9f34b9796e3d4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] | |
| //snippet-sourceauthor:[Doug-AWS] | |
| //snippet-sourcedescription:[Creates a KMS key.] | |
| //snippet-keyword:[AWS Key Management Service] | |
| //snippet-keyword:[CreateKey function] | |
| //snippet-keyword:[Go] | |
| //snippet-service:[kms] | |
| //snippet-sourcetype:[full-example] | |
| //snippet-sourcedate:[2018-03-16] | |
| /* | |
| Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| This file is licensed under the Apache License, Version 2.0 (the "License"). | |
| You may not use this file except in compliance with the License. A copy of | |
| the License is located at | |
| http://aws.amazon.com/apache2.0/ | |
| This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
| CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations under the License. | |
| */ | |
| package main | |
| import ( | |
| "github.com/aws/aws-sdk-go/aws" | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/kms" | |
| "fmt" | |
| "os" | |
| ) | |
| // Create a customer master key (CMK) | |
| // Since we are only encrypting small amounts of data (4 KiB or less) directly, | |
| // a CMK is fine for our purposes. | |
| // For larger amounts of data, | |
| // use the CMK to encrypt a data encryption key (DEK). | |
| func main() { | |
| // Initialize a session in us-west-2 that the SDK will use to load | |
| // credentials from the shared credentials file ~/.aws/credentials. | |
| sess, err := session.NewSession(&aws.Config{ | |
| Region: aws.String("us-west-2")}, | |
| ) | |
| // Create KMS service client | |
| svc := kms.New(sess) | |
| // Create the key | |
| result, err := svc.CreateKey(&kms.CreateKeyInput{ | |
| Tags: []*kms.Tag{ | |
| { | |
| TagKey: aws.String("CreatedBy"), | |
| TagValue: aws.String("ExampleUser"), | |
| }, | |
| }, | |
| }) | |
| if err != nil { | |
| fmt.Println("Got error creating key: ", err) | |
| os.Exit(1) | |
| } | |
| fmt.Println(*result.KeyMetadata.KeyId) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] | |
| //snippet-sourceauthor:[Doug-AWS] | |
| //snippet-sourcedescription:[Decrypts a string that was encrypted by KMS.] | |
| //snippet-keyword:[AWS Key Management Service] | |
| //snippet-keyword:[Decrypt function] | |
| //snippet-keyword:[Go] | |
| //snippet-service:[kms] | |
| //snippet-sourcetype:[full-example] | |
| //snippet-sourcedate:[2018-03-16] | |
| /* | |
| Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| This file is licensed under the Apache License, Version 2.0 (the "License"). | |
| You may not use this file except in compliance with the License. A copy of | |
| the License is located at | |
| http://aws.amazon.com/apache2.0/ | |
| This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
| CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations under the License. | |
| */ | |
| package main | |
| import ( | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/kms" | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| // Initialize a session in us-west-2 that the SDK will use to load | |
| // credentials from the shared credentials file ~/.aws/credentials. | |
| sess, err := session.NewSession(&aws.Config{ | |
| Region: aws.String("us-west-2")}, | |
| ) | |
| // Create KMS service client | |
| svc := kms.New(sess) | |
| // Encrypted data | |
| blob := []byte{...} | |
| // Decrypt the data | |
| result, err := svc.Decrypt(&kms.DecryptInput{CiphertextBlob: blob}) | |
| if err != nil { | |
| fmt.Println("Got error decrypting data: ", err) | |
| os.Exit(1) | |
| } | |
| blob_string := string(result.Plaintext) | |
| fmt.Println(blob_string) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] | |
| //snippet-sourceauthor:[Doug-AWS] | |
| //snippet-sourcedescription:[Encrypts a string using KMS.] | |
| //snippet-keyword:[AWS Key Management Service] | |
| //snippet-keyword:[Encrypt function] | |
| //snippet-keyword:[Go] | |
| //snippet-service:[kms] | |
| //snippet-sourcetype:[full-example] | |
| //snippet-sourcedate:[2018-03-16] | |
| /* | |
| Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| This file is licensed under the Apache License, Version 2.0 (the "License"). | |
| You may not use this file except in compliance with the License. A copy of | |
| the License is located at | |
| http://aws.amazon.com/apache2.0/ | |
| This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
| CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations under the License. | |
| */ | |
| package main | |
| import ( | |
| "github.com/aws/aws-sdk-go/aws" | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/kms" | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| // Initialize a session in us-west-2 that the SDK will use to load | |
| // credentials from the shared credentials file ~/.aws/credentials. | |
| sess, err := session.NewSession(&aws.Config{ | |
| Region: aws.String("us-west-2")}, | |
| ) | |
| // Create KMS service client | |
| svc := kms.New(sess) | |
| // Encrypt data key | |
| // | |
| // Replace the fictitious key ARN with a valid key ID | |
| keyId := "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" | |
| text := "1234567890" | |
| // Encrypt the data | |
| result, err := svc.Encrypt(&kms.EncryptInput{ | |
| KeyId: aws.String(keyId), | |
| Plaintext: []byte(text), | |
| }) | |
| if err != nil { | |
| fmt.Println("Got error encrypting data: ", err) | |
| os.Exit(1) | |
| } | |
| fmt.Println("Blob (base-64 byte array):") | |
| fmt.Println(result.CiphertextBlob) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.] | |
| //snippet-sourceauthor:[Doug-AWS] | |
| //snippet-sourcedescription:[Decrypts encrypted data and then immediately re-encrypts data under a new customer master key (CMK).] | |
| //snippet-keyword:[AWS Key Management Service] | |
| //snippet-keyword:[ReEncrypt function] | |
| //snippet-keyword:[Go] | |
| //snippet-service:[kms] | |
| //snippet-sourcetype:[full-example] | |
| //snippet-sourcedate:[2018-03-16] | |
| /* | |
| Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
| This file is licensed under the Apache License, Version 2.0 (the "License"). | |
| You may not use this file except in compliance with the License. A copy of | |
| the License is located at | |
| http://aws.amazon.com/apache2.0/ | |
| This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
| CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations under the License. | |
| */ | |
| package main | |
| import ( | |
| "github.com/aws/aws-sdk-go/aws/session" | |
| "github.com/aws/aws-sdk-go/service/kms" | |
| "fmt" | |
| "os" | |
| ) | |
| func main() { | |
| // Initialize a session in us-west-2 that the SDK will use to load | |
| // credentials from the shared credentials file ~/.aws/credentials. | |
| sess, err := session.NewSession(&aws.Config{ | |
| Region: aws.String("us-west-2")}, | |
| ) | |
| // Create KMS service client | |
| svc := kms.New(sess) | |
| // Encrypt data key | |
| // | |
| // Replace the fictitious key ARN with a valid key ID | |
| keyId := "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" | |
| // Encrypted data | |
| blob := []byte{...} | |
| // Re-encrypt the data key | |
| result, err := svc.ReEncrypt(&kms.ReEncryptInput{CiphertextBlob: blob, DestinationKeyId: &keyId}) | |
| if err != nil { | |
| fmt.Println("Got error re-encrypting data: ", err) | |
| os.Exit(1) | |
| } | |
| fmt.Println("Blob (base-64 byte array):") | |
| fmt.Println(result.CiphertextBlob) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment