Last active
January 3, 2017 08:52
-
-
Save gouf/7fe0f6714fd26ecdb6eb7759465ce728 to your computer and use it in GitHub Desktop.
AWS KMS サービスを利用して、秘密にしたいデータをコードの中で扱えるようにする
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
require 'aws-sdk' | |
# Please set your own key id | |
# Ref: https://console.aws.amazon.com/iam/home?region=ap-northeast-1#/encryptionKeys/ap-northeast-1 | |
key_id = 'arn:aws:kms:ap-northeast-1:000000000000:key/00000000-0000-0000-0000-000000000000' # note: keep secret key_id, export to environment variable | |
kms = Aws::KMS::Client.new( | |
region: 'ap-northeast-1' | |
) | |
response = kms.encrypt( | |
key_id: key_id, | |
plaintext: 'my_sensitive_text_data' | |
) | |
p ciphertext_blob = response.dig(:ciphertext_blob).to_s | |
# => "\x01\x01\x02\x00xS\xBB\xBE\xC5\x03G\xE9>fz{\xBEyW\x8E&\x01I\xFB\xBF\xBD\xB0Vfk(snip)... | |
response2 = kms.decrypt( | |
ciphertext_blob: ciphertext_blob | |
) | |
p response2.dig(:plaintext) | |
# => 'my_sensitive_text_data' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment