Create This model in your Model directory : where ever you want to use just include PublicAccessViewHelper
and call encrypt and decrypt method for encryption code and decrypting secret key
require 'openssl'require 'base64'require 'addressable/uri'require 'uri'
module PublicAccessViewHelper
def self.included(base)
base.extend self end
def cipher
OpenSSL::Cipher::Cipher.new('aes-256-cbc') # ('aes-256-cbc') end
def cipher_key '@!\xF\xCD\x\x18d\xFD\xFx91\xD4\x03\x92ym\x8F\xD3\x807\xC3\98\xE0\x8E\x8F\xD5T$T\x1CAF\x1Fc\tZE\xE0\x04\xD5\xD7|(J\x8B\xE14\x03S\xF3\xE2y\\X\x9E' end
def decrypt(encrypted_data)
=begin c = cipher.decrypt c.key = Digest::SHA256.digest(cipher_key) URI.encode(c.update(Base64.decode64(value.to_s)) + c.final)
=end # URI.decode(value) # salt = SecureRandom.random_bytes(64) # key = ActiveSupport::KeyGenerator.new('password').generate_key(salt) # => "\x89\xE0\x156\xAC..." crypt = ActiveSupport::MessageEncryptor.new(cipher_key)
crypt.decrypt_and_verify(encrypted_data)
end
def encrypt(secret_data)
crypt = ActiveSupport::MessageEncryptor.new(cipher_key)
encrypted_data = crypt.encrypt_and_sign(secret_data)
return encrypted_data=begin c = cipher.encrypt c.key = Digest::SHA256.digest(cipher_key) URI.encode(Base64.encode64(c.update(value.to_s) + c.final)) URI.encode(value)=end end
def encrypt_url(value)
c = cipher.encrypt
c.key = Digest::SHA256.digest(cipher_key)
Base64.encode64(c.update(value.to_s) + c.final)
end
end
No comments:
Post a Comment