Sha256: 2b290fe609beee4306e9ae92899d263b9a628d05f7983a4301db9ec0e27c93fc

Contents?: true

Size: 900 Bytes

Versions: 2

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true

require "base64"
require "json"
require_relative "payload"

module ROM
  module EncryptedAttribute
    class Decryptor
      def initialize(derivator:)
        @derivator = derivator
      end

      def decrypt(message)
        payload = ROM::EncryptedAttribute::Payload.decode(message)

        cipher = OpenSSL::Cipher.new("aes-256-gcm")
        key = @derivator.derive(cipher.key_len)

        cipher.decrypt
        cipher.padding = 0
        cipher.key = key
        cipher.iv = payload.initialization_vector
        cipher.auth_tag = payload.auth_tag
        cipher.auth_data = ""
        cipher.update(payload.message) + cipher.final
      rescue JSON::ParserError
        # we need to unconditionally support of reading unencrypted data due to a bug in rom-sql
        # https://github.com/rom-rb/rom-sql/issues/423
        message
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-encrypted_attribute-0.0.4 lib/rom/encrypted_attribute/decryptor.rb
rom-encrypted_attribute-0.0.3 lib/rom/encrypted_attribute/decryptor.rb