Skip to content

Instantly share code, notes, and snippets.

@csexton
Created July 22, 2020 14:24
Show Gist options
  • Save csexton/71c589c4262359987cea1eac580dce2b to your computer and use it in GitHub Desktop.
Save csexton/71c589c4262359987cea1eac580dce2b to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
module Encryptable
ENCRYPTABLE_SALT = "iris.encryptable_salt"
def encrypt(string)
crypt.encrypt_and_sign(string)
end
def decrypt(encrypted_data)
crypt.decrypt_and_verify(encrypted_data)
end
private
def crypt
secret = Rails.application.secrets.secret_key_base
len = ActiveSupport::MessageEncryptor.key_len
salt = ENCRYPTABLE_SALT
key = ActiveSupport::KeyGenerator.new(secret).generate_key(salt, len)
ActiveSupport::MessageEncryptor.new(key)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment