Class: Decrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/encrypt_column/decrypt.rb

Class Method Summary collapse

Class Method Details

.cipher(ciphertext, key = ENV['ENCRYPTION_KEY']) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/encrypt_column/decrypt.rb', line 3

def self.cipher(ciphertext, key = ENV['ENCRYPTION_KEY'])
  raise 'Encryption Key Config Missing' unless key.present?
  ActiveSupport::MessageEncryptor.new(key).decrypt_and_verify(ciphertext)
rescue ActiveSupport::MessageVerifier::InvalidSignature
  return 'ERROR: Missing encryption ciphertext' if ciphertext.nil? || ciphertext.blank?
  return 'ERROR: Wrong encryption key specified'
end

.ciphertext(ciphertext, key = ENV['ENCRYPT_KEY']) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/encrypt_column/decrypt.rb', line 11

def self.ciphertext(ciphertext, key = ENV['ENCRYPT_KEY'])
  raise 'Encryption Key Config Missing' unless key.present?
  return 'ERROR: Missing encryption ciphertext' if ciphertext.nil? || ciphertext.blank?
  enciphered, iv = ciphertext.split('--', 2).map { |part| part.unpack('m')[0] }
  decipher = OpenSSL::Cipher::AES256.new(:CBC)

  decipher.decrypt
  decipher.key = key
  decipher.iv = iv

  deciphered = decipher.update(enciphered)
  deciphered << decipher.final
end