Class: Encrypt

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

Class Method Summary collapse

Class Method Details

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



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/encrypt_column/encrypt.rb', line 9

def self.plaintext(plaintext, key = ENV['ENCRYPT_KEY'])
  return raise 'Missing Encryption Key Config' if key.nil?
  cipher = OpenSSL::Cipher::AES256.new(:CBC)
  iv = cipher.random_iv

  cipher.encrypt
  cipher.key = key
  cipher.iv = iv

  enciphered = cipher.update(plaintext)
  enciphered << cipher.final
  [enciphered, iv].map { |part| [part].pack('m').gsub(/\n/, '') }.join('--')
end

.text(plaintext, key = ENV['ENCRYPTION_KEY']) ⇒ Object



4
5
6
7
# File 'lib/encrypt_column/encrypt.rb', line 4

def self.text(plaintext, key = ENV['ENCRYPTION_KEY'])
  return raise 'Missing Encryption Key Config' if key.nil?
  ActiveSupport::MessageEncryptor.new(key).encrypt_and_sign(plaintext)
end