Created
July 3, 2017 21:13
-
-
Save mecampbellsoup/a166950670d0c3a1970b83d236f10423 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
| class SensitiveValue | |
| attr_reader :value, :encrypted_value | |
| def initialize(value) | |
| @value = value | |
| end | |
| def encrypt | |
| bin = value.unpack("B*") | |
| encoded_bin = Base64.encode64(bin[0]) | |
| resp = client.encrypt( | |
| key_id: ENV.fetch("AWS_CMK_IDENTIFIER"), | |
| plaintext: encoded_bin | |
| ) | |
| @encrypted_value = resp.to_h[:ciphertext_blob] | |
| end | |
| def decrypt | |
| bin = Base64.decode64(encrypted_value) | |
| # Returns a base64 encoded plaintext string | |
| resp = client.decrypt(ciphertext_blob: [bin].pack("B*")) | |
| resp.plaintext | |
| end | |
| private | |
| def client | |
| # NOTE: see config/initializers/aws.rb for configuration details. | |
| @client ||= Aws::KMS::Client.new | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment