Sha256: fc8054476e9d8ef07a8957c63d06f74bd95566ff5ba8d5d2001e52d48e51d602
Contents?: true
Size: 1.37 KB
Versions: 22
Compression:
Stored size: 1.37 KB
Contents
require 'base64' module Flydata module Util class Encryptor SALT = 'ae8k9h10' class << self def encrypt(text, key) validate_presence(text: text, key: key) cipher = build_cipher(:encrypt, key) Base64.encode64(cipher.update(text) + cipher.final) end def decrypt(text, key, param_name = nil) decrypt!(text, key, param_name) rescue => e text end def decrypt!(text, key, param_name = nil) validate_presence(text: text, key: key) cipher = build_cipher(:decrypt, key) cipher.update(Base64.decode64(text)) + cipher.final rescue OpenSSL::Cipher::CipherError => e raise "Failed to decrypt '#{param_name}' parameter. error:'#{e.to_s}'" end private def validate_presence(names) names.each do |name, value| raise ArgumentError.new("#{name} is required.") if value.nil? or value.empty? end end def build_cipher(type, key) cipher = OpenSSL::Cipher::Cipher.new('AES-128-CBC') case type when :encrypt cipher.encrypt when :decrypt cipher.decrypt else raise "Invalid type: #{type}" end cipher.pkcs5_keyivgen(key, SALT) cipher end end end end end
Version data entries
22 entries across 22 versions & 1 rubygems