882090fc |
require 'bcrypt'
module Security
mattr_reader :cipher_iv, :cipher_key
def self.encrypt(unencrypted_message, password = nil)
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.encrypt()
pass = password ? password : generate_token()
cipher.key = @@cipher_key = Digest::SHA1.hexdigest(pass)
cipher.iv = @@cipher_iv = cipher.random_iv
encrypted = cipher.update(unencrypted_message)
encrypted << cipher.final
end
def self.decrypt(encrypted_message, key, iv)
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.decrypt()
cipher.key = @@cipher_key = key
cipher.iv = @@cipher_iv = iv
|