Class: KmsTools::Decrypter
Overview
Provides low-level decryption functionality for kms-tools
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#decrypt_string(str) ⇒ String
Decrypt base64 encoded ciphertext that was encrypted directly with a customer master key.
-
#decrypt_with_data_key(params) ⇒ String
Decrypt a blob using private keys.
-
#integrity_verified?(data, checksum) ⇒ Boolean
Verify data blob against known hash.
-
#stream_decrypt_with_data_key(params) ⇒ Object
Decrypt a stream using private keys.
Methods inherited from Base
#available_aliases, #from_64, #initialize, #master_key_arn, #master_key_id, #region, #to_64, #to_s64, #use_key_alias=
Constructor Details
This class inherits a constructor from KmsTools::Base
Instance Method Details
#decrypt_string(str) ⇒ String
Decrypt base64 encoded ciphertext that was encrypted directly with a customer master key
10 11 12 |
# File 'lib/kms-tools/decrypter.rb', line 10 def decrypt_string(str) kms.decrypt({:ciphertext_blob => from_64(str)}).plaintext end |
#decrypt_with_data_key(params) ⇒ String
Decrypt a blob using private keys
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kms-tools/decrypter.rb', line 22 def decrypt_with_data_key(params) cipher = OpenSSL::Cipher.new(params[:cipher]) cipher.decrypt cipher.key = decrypt_string(params[:encrypted_key]) cipher.iv = decrypt_string(params[:encrypted_iv]) decrypted_data = cipher.update(params[:encrypted_data]) + cipher.final raise "File integrity check failed!" unless integrity_verified?(decrypted_data, params[:checksum]) decrypted_data end |
#integrity_verified?(data, checksum) ⇒ Boolean
Verify data blob against known hash
77 78 79 |
# File 'lib/kms-tools/decrypter.rb', line 77 def integrity_verified?(data, checksum) Digest::SHA1.hexdigest(data).eql? checksum end |
#stream_decrypt_with_data_key(params) ⇒ Object
Decrypt a stream using private keys
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kms-tools/decrypter.rb', line 44 def stream_decrypt_with_data_key(params) # set up cipher cipher = OpenSSL::Cipher.new(params[:cipher]) cipher.decrypt cipher.key = decrypt_string(params[:encrypted_key]) cipher.iv = decrypt_string(params[:encrypted_iv]) sha1 = Digest::SHA1.new if params[:checksum] # write the output stream chunk = "" params[:in].seek(params[:position], IO::SEEK_SET) if params[:position] while params[:in].read(STREAM_CHUNK_SIZE, chunk) decrypted_chunk = cipher.update(chunk) sha1.update(decrypted_chunk) if params[:checksum] params[:out] << decrypted_chunk end final = cipher.final sha1.update(final) if params[:checksum] if params[:checksum] raise "Decrypted data stream failed checksum verification!" unless params[:checksum].eql? sha1.hexdigest end params[:out] << final true end |