Created
December 13, 2021 22:25
-
-
Save JamesAndresCM/2e2a4c9b2fa1bb6b954aa4a9fd755066 to your computer and use it in GitHub Desktop.
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
# first way | |
# frozen_string_literal: true | |
class EncryptionService | |
delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor | |
class << self | |
def encrypt(secret:) | |
new.encrypt_and_sign(secret) | |
end | |
def decrypt(secret:) | |
return unless secret | |
new.decrypt_and_verify(secret) | |
rescue ActiveSupport::MessageEncryptor::InvalidMessage | |
nil | |
end | |
end | |
private | |
def encryptor | |
ActiveSupport::MessageEncryptor.new(KEY) | |
end | |
end | |
# second way | |
# frozen_string_literal: true | |
class EncryptionService | |
class << self | |
def encrypt(secret:) | |
encrypt_and_sign(secret) | |
end | |
def decrypt(secret:) | |
return unless secret | |
decrypt_and_verify(secret) | |
rescue ActiveSupport::MessageEncryptor::InvalidMessage | |
nil | |
end | |
def encryptor | |
ActiveSupport::MessageEncryptor.new(KEY) | |
end | |
delegate :encrypt_and_sign, :decrypt_and_verify, to: :encryptor | |
end | |
private_class_method :encryptor | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment