Module: DecryptionHandler

Defined in:
lib/encryptable/decryption_handler.rb

Overview

decryption_handler.rb DecryptionHandler

Class Method Summary collapse

Class Method Details

.decrypt(value, key, iv, algorithm) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/encryptable/decryption_handler.rb', line 8

def self.decrypt(value, key, iv, algorithm)
  return if value.nil?

  decoded = decode_input(value)
  validate_decoded_input!(decoded)

  cipher = setup_cipher(key, iv, algorithm)
  decrypted_value = decrypt_value(cipher, decoded)

  decrypted_value
rescue OpenSSL::Cipher::CipherError => e
  handle_decryption_error(e)
rescue ArgumentError => e
  if e.message.match?(/invalid base64/)
    raise 'Decryption failed: The input data is not a valid Base64 encoded string.'
  else
    raise e
  end
end

.validate_decoded_input!(decoded) ⇒ Object



28
29
30
31
32
# File 'lib/encryptable/decryption_handler.rb', line 28

def self.validate_decoded_input!(decoded)
  if decoded.bytesize % 16 != 0
    raise 'Decryption failed: The input data is not a valid Base64 encoded string.'
  end
end