Sha256: 4cc5bb1a12b07755be5d11d32d4632a1bdeccc7fa7eae300c0d188043e5f410e

Contents?: true

Size: 984 Bytes

Versions: 14

Compression:

Stored size: 984 Bytes

Contents

module KmsEncrypted
  module Clients
    class Aws < Base
      def encrypt(plaintext, context: nil)
        options = {
          key_id: key_id,
          plaintext: plaintext
        }
        options[:encryption_context] = generate_context(context) if context

        KmsEncrypted.aws_client.encrypt(options).ciphertext_blob
      end

      def decrypt(ciphertext, context: nil)
        options = {
          ciphertext_blob: ciphertext
        }
        options[:encryption_context] = generate_context(context) if context

        begin
          KmsEncrypted.aws_client.decrypt(options).plaintext
        rescue ::Aws::KMS::Errors::InvalidCiphertextException
          decryption_failed!
        end
      end

      private

      # make integers strings for convenience
      def generate_context(context)
        raise ArgumentError, "Context must be a hash" unless context.is_a?(Hash)
        Hash[context.map { |k, v| [k, context_value(v)] }]
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kms_encrypted-1.6.0 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.5.1 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.5.0 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.4.0 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.3.0 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.2.4 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.2.3 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.2.2 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.2.1 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.2.0 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.1.1 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.1.0 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.0.1 lib/kms_encrypted/clients/aws.rb
kms_encrypted-1.0.0 lib/kms_encrypted/clients/aws.rb