module Encryptor
def self.crypt(cipher_method, *args) #:nodoc:
def self.crypt(cipher_method, *args) #:nodoc: options = default_options.merge(:value => args.first).merge(args.last.is_a?(Hash) ? args.last : {}) cipher = OpenSSL::Cipher::Cipher.new(options[:algorithm]) cipher.send(cipher_method) if options[:iv] cipher.key = options[:key] cipher.iv = options[:iv] else cipher.pkcs5_keyivgen(options[:key]) end result = cipher.update(options[:value]) result << cipher.final end
def self.decrypt(*args)
# or
decrypted_value = Encryptor.decrypt(:value => 'some encrypted string', :key => 'some secret key')
Example
Optionally accepts :iv and :algorithm options
Decrypts a :value with a specified :key
def self.decrypt(*args) crypt :decrypt, *args end
def self.default_options
Defaults to { :algorithm => 'aes-256-cbc' }
The default options to use when calling the encrypt and decrypt methods
def self.default_options @default_options ||= { :algorithm => 'aes-256-cbc' } end
def self.encrypt(*args)
# or
encrypted_value = Encryptor.encrypt(:value => 'some string to encrypt', :key => 'some secret key')
Example
Optionally accepts :iv and :algorithm options
Encrypts a :value with a specified :key
def self.encrypt(*args) crypt :encrypt, *args end