Skip to content

Instantly share code, notes, and snippets.

@JamesAndresCM
Created December 13, 2021 22:25
Show Gist options
  • Save JamesAndresCM/2e2a4c9b2fa1bb6b954aa4a9fd755066 to your computer and use it in GitHub Desktop.
Save JamesAndresCM/2e2a4c9b2fa1bb6b954aa4a9fd755066 to your computer and use it in GitHub Desktop.
# 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