Sha256: 7ca93f40e767c5af6775003747c364570caf00a2c6b36f72dfc2cb69232f029c

Contents?: true

Size: 1008 Bytes

Versions: 4

Compression:

Stored size: 1008 Bytes

Contents

module Avo
  module Services
    class EncryptionService
      attr_reader :message
      attr_reader :purpose
      attr_reader :crypt

      class << self
        def encrypt(message:, purpose:)
          new(message: message, purpose: purpose).encrypt
        end

        def decrypt(message:, purpose:)
          new(message: message, purpose: purpose).decrypt
        end
      end

      def initialize(message:, purpose:)
        @message = message
        @purpose = purpose
        @crypt = ActiveSupport::MessageEncryptor.new(encryption_key)
      end

      def encrypt
        crypt.encrypt_and_sign(message, purpose: purpose)
      end

      def decrypt
        crypt.decrypt_and_verify(message, purpose: purpose)
      end

      private

      def encryption_key
        Rails.application.secret_key_base[0..31]
      rescue
        # This will fail the decryption process.
        # It's here only to keep Avo from crashing
        SecureRandom.random_bytes(32)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
avo-2.53.0 lib/avo/services/encryption_service.rb
avo-2.52.0 lib/avo/services/encryption_service.rb
avo-2.49.0 lib/avo/services/encryption_service.rb
avo-2.48.0 lib/avo/services/encryption_service.rb