Sha256: 504a72d1d806f556451d3b0db5c3d6d6b072f371e585e526fbdda0e27ca620f6
Contents?: true
Size: 1.05 KB
Versions: 64
Compression:
Stored size: 1.05 KB
Contents
# # A mixin to help with encrypting data in a secure way # module CipherAble extend ActiveSupport::Concern # Add to the model def self.included(base) base.class_eval do # # Fields # field :secret_key, type: BSON::Binary end end # # Encrypt the given text # def encrypt(text) cipher.encrypt_and_sign(text) rescue StandardError => error App47Logger.log_error("Unable to encrypt text for #{inspect}", error) nil end # # Decrypt the given text # def decrypt(text) cipher.decrypt_and_verify(text) rescue StandardError => error App47Logger.log_warn("Unable to decrypt text for #{inspect}", error) nil end # # Get the cipher directly # def cipher generate_key if secret_key.blank? ActiveSupport::MessageEncryptor.new(secret_key.data) end private def generate_key len = ActiveSupport::MessageEncryptor.key_len salt = SecureRandom.random_bytes(len) set secret_key: BSON::Binary.new(ActiveSupport::KeyGenerator.new(id.to_s).generate_key(salt, len)) end end
Version data entries
64 entries across 64 versions & 1 rubygems