View a markdown version of this page

AWS Encryption SDK Go용 예제 코드 - AWS Encryption SDK

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS Encryption SDK Go용 예제 코드

다음 예제에서는 AWS Encryption SDK for Go로 프로그래밍할 때 사용하는 기본 코딩 패턴을 보여줍니다. 특히 AWS Encryption SDK 및 재료 공급자 라이브러리를 인스턴스화합니다. 그런 다음 각 메서드를 호출하기 전에 메서드에 대한 입력을 정의하는 객체를 인스턴스화합니다.

대체 알고리즘 제품군 지정 및 암호화된 데이터 키 제한 AWS Encryption SDK과 같은 옵션을에서 구성하는 방법을 보여주는 예제는 GitHub의 aws-encryption-sdk 리포지토리에 있는 Go 예제를 참조하세요.

AWS Encryption SDK for Go에서 데이터 암호화 및 복호화

이 예제에서는 데이터 암호화 및 복호화의 기본 패턴을 보여줍니다. 하나의 AWS KMS 래핑 키로 보호되는 데이터 키로 소량의 데이터를 암호화합니다.

1단계: 인스턴스화 AWS Encryption SDK.

의 메서드를 사용하여 데이터를 암호화하고 복호화 AWS Encryption SDK 합니다.

import ( "context" mpl "aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygenerated" mpltypes "aws/aws-cryptographic-material-providers-library/releases/go/mpl/awscryptographymaterialproviderssmithygeneratedtypes" client "github.com/aws/aws-encryption-sdk/awscryptographyencryptionsdksmithygenerated" esdktypes "github.com/aws/aws-encryption-sdk/awscryptographyencryptionsdksmithygeneratedtypes" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/kms" ) encryptionClient, err := client.NewClient(esdktypes.AwsEncryptionSdkConfig{}) if err != nil { panic(err) }
2단계: AWS KMS 클라이언트를 생성합니다.
cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic(err) } kmsClient := kms.NewFromConfig(cfg, func(o *kms.Options) { o.Region = KmsKeyRegion })
선택 사항: 암호화 컨텍스트를 생성합니다.
encryptionContext := map[string]string{ "encryption": "context", "is not": "secret", "but adds": "useful metadata", "that can help you": "be confident that", "the data you are handling": "is what you think it is", }
3단계: 재료 공급자 라이브러리를 인스턴스화합니다.

구성 요소 공급자 라이브러리의 메서드를 사용하여, 데이터를 보호하는 키를 지정하는 키링을 만들 수 있습니다.

matProv, err := mpl.NewClient(mpltypes.MaterialProvidersConfig{}) if err != nil { panic(err) }
4단계: AWS KMS 키링을 생성합니다.

키링을 생성하려면 키링 입력 객체를 사용하여 키링 메서드를 호출합니다. 이 예제에서는 CreateAwsKmsKeyring 메서드를 사용하고 KMS 키 하나를 지정합니다. kmsKeyId 변수는 사용자가 제공하는 KMS 키의 키 ARN을 나타냅니다.

awsKmsKeyringInput := mpltypes.CreateAwsKmsKeyringInput{ KmsClient: kmsClient, KmsKeyId: kmsKeyId, } awsKmsKeyring, err := matProv.CreateAwsKmsKeyring(context.Background(), awsKmsKeyringInput) if err != nil { panic(err) }
5단계: 일반 텍스트를 암호화합니다.
res, err := encryptionClient.Encrypt(context.Background(), esdktypes.EncryptInput{ Plaintext: []byte(exampleText), EncryptionContext: encryptionContext, Keyring: awsKmsKeyring, }) if err != nil { panic(err) } ciphertext := res.Ciphertext
6단계: 암호화에 사용한 것과 동일한 키링을 사용하여 암호화된 데이터를 복호화합니다.
decryptOutput, err := encryptionClient.Decrypt(context.Background(), esdktypes.DecryptInput{ Ciphertext: ciphertext, // Provide the encryption context that was supplied to the encrypt method EncryptionContext: encryptionContext, Keyring: awsKmsKeyring, }) if err != nil { panic(err) } decrypted := decryptOutput.Plaintext