Skip to content

Instantly share code, notes, and snippets.

@jigneshkhokhani
Created February 23, 2018 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jigneshkhokhani/150f84dfaa2d3b30e887c3dfe708f1da to your computer and use it in GitHub Desktop.
Save jigneshkhokhani/150f84dfaa2d3b30e887c3dfe708f1da to your computer and use it in GitHub Desktop.
[Encrypt/Decrypt] Encrypt and decrypt rails data
# 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