Created
February 23, 2018 15:20
-
-
Save jigneshkhokhani/150f84dfaa2d3b30e887c3dfe708f1da to your computer and use it in GitHub Desktop.
[Encrypt/Decrypt] Encrypt and decrypt rails data
This file contains 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
# lib/crypt.rb | |
module Crypt | |
class << self | |
def encrypt(value) | |
crypt(:encrypt, value) | |
end | |
def decrypt(value) | |
crypt(:decrypt, value) | |
end | |
def encryption_key | |
ENV['ENCRYPTION_KEY'] | |
end | |
ALGO = 'aes-256-cbc'.freeze | |
def crypt(cipher_method, value) | |
cipher = OpenSSL::Cipher.new(ALGO) | |
cipher.send(cipher_method) | |
cipher.pkcs5_keyivgen(encryption_key) | |
result = cipher.update(value) | |
result << cipher.final | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment