module DataEncryptor

Constants

VERSION

Public Class Methods

decrypt(encrypted_data) click to toggle source
# File lib/data_encryptor.rb, line 25
def self.decrypt(encrypted_data)
  raise ArgumentError, 'must specify a key' if @options[:key].to_s.empty?

  encrypted_data, iv = encrypted_data.split('--').map { |v| ::Base64.strict_decode64(v) }

  raise ArgumentError, 'encrypted data has invalid format' unless encrypted_data && iv

  cipher = new_cipher(method: :decrypt)
  cipher.iv = iv

  decrypted_data = cipher.update(encrypted_data)
  decrypted_data << cipher.final

  JSON.parse(decrypted_data)
end
encrypt(data) click to toggle source
# File lib/data_encryptor.rb, line 14
def self.encrypt(data)
  raise ArgumentError, 'must specify a key' if @options[:key].to_s.empty?

  cipher = new_cipher
  iv = cipher.random_iv

  decrypted = cipher.update(JSON.dump(data))
  decrypted << cipher.final
  "#{::Base64.strict_encode64(decrypted)}--#{::Base64.strict_encode64(iv)}"
end
setup!(key:, algorithm: 'AES-256-CBC') click to toggle source
# File lib/data_encryptor.rb, line 10
def self.setup!(key:, algorithm: 'AES-256-CBC')
  @options = { key: key, algorithm: algorithm }
end

Private Class Methods

new_cipher(method: :encrypt) click to toggle source
# File lib/data_encryptor.rb, line 41
                     def self.new_cipher(method: :encrypt)
  cipher = OpenSSL::Cipher.new(@options.fetch(:algorithm)).public_send(method)
  cipher.key = Digest::SHA256.digest @options.fetch(:key)
  cipher
end