Created
December 9, 2021 21:40
-
-
Save rafaelqueiroz88/1f68e5da77c95c57ef09350f97e6e8f7 to your computer and use it in GitHub Desktop.
ID encryptor and decryptor
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
# app/lib/encryptor.rb | |
class Encryptor | |
def self.encrypt(unencrypted_string) | |
_cipher = OpenSSL::Cipher.new('AES-256-CBC').encrypt | |
_cipher.key = "AtafSyyodFgHwvbGNFtekoRKarGvMJlu" | |
if unencrypted_string.present? | |
encrypted_string = _cipher.update(unencrypted_string) + _cipher.final | |
Base64.encode64(encrypted_string.unpack('H*')[0].upcase).chomp | |
else | |
encrypted_string = "" | |
end | |
end | |
def self.decrypt(encrypted_string) | |
_cipher = OpenSSL::Cipher.new('AES-256-CBC').decrypt | |
_cipher.key = "AtafSyyodFgHwvbGNFtekoRKarGvMJlu" | |
encrypted_string = [Base64.decode64(encrypted_string)].pack("H*") | |
if encrypted_string.present? | |
unencrypted_string = _cipher.update(encrypted_string) + _cipher.final | |
else | |
unencrypted_string = "" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment