DecryptData.go - AWS Code Sample

DecryptData.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main 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" ) // DecodeData decrypts some text that was encrypted with an AWS Key Management Service (AWS KMS) customer master key (CMK). // Inputs: // svc is an AWS KMS service client // blob is an array of bytes containing the text to decrypt // Output: // If success, information about the text and nil // Otherwise, nil and an error from the call to Decrypt func DecodeData(svc kmsiface.KMSAPI, blob *[]byte) (*kms.DecryptOutput, error) { result, err := svc.Decrypt(&kms.DecryptInput{ CiphertextBlob: *blob, }) if err != nil { return nil, err } return result, err } func main() { data := flag.String("d", "", "The encrypted data, as a string") flag.Parse() if *data == "" { fmt.Println("You must supply the encrypted data as a string") fmt.Println("-d DATA") return } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := kms.New(sess) blob := []byte(*data) result, err := DecodeData(svc, &blob) if err != nil { fmt.Println("Got error decrypting data: ", err) return } fmt.Println(string(result.Plaintext)) }