Created
July 22, 2020 14:24
-
-
Save csexton/71c589c4262359987cea1eac580dce2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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