Skip to content

Instantly share code, notes, and snippets.

@ankitsinghaniyaz
Created March 18, 2020 08:39
Show Gist options
  • Save ankitsinghaniyaz/95f9fe585d7c5291347be65961a408c0 to your computer and use it in GitHub Desktop.
Save ankitsinghaniyaz/95f9fe585d7c5291347be65961a408c0 to your computer and use it in GitHub Desktop.
A simpler herlper library to encrypt and decrypt values
# Encryptor - is a library responsible for ecnrypting and decrypting values
# Inspired from - https://gist.github.com/wteuber/5318013
require 'openssl'
class Encryptor
SECRET_KEY = Rails.application.secrets.secret_key_base
def self.encrypt(text)
cipher = OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
cipher.key = Digest::SHA1.hexdigest(SECRET_KEY)
s = cipher.update(text) + cipher.final
s.unpack('H*')[0].upcase
end
def self.decrypt(text)
cipher = OpenSSL::Cipher.new('DES-EDE3-CBC').decrypt
cipher.key = Digest::SHA1.hexdigest(SECRET_KEY)
s = [text].pack("H*").unpack("C*").pack("c*")
cipher.update(s) + cipher.final
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment