# frozen_string_literal: true module Pubnub module Crypto # Encrypted data representation object. # # Objects contain both encrypted data and additional data created by cryptor # that will be required to decrypt the data. class EncryptedData # Cryptor may provide here any information which will be useful when data # should be decrypted. # # For example metadata may contain: # * initialization vector # * cipher key identifier # * encrypted data length # # @return [String, nil] Cryptor-defined information. attr_reader :metadata # Encrypted data. # # @return [String] Encrypted data. attr_reader :data # Create encrypted data object. # # An object used to keep track of the results of data encryption and the # additional data the cryptor needs to handle it later. # # @param data [String] Outcome of successful cryptor encrypt method # call. # @param metadata [String, nil] Additional information is provided by # cryptor so that encrypted data can be handled later. def initialize(data, metadata = nil) @data = data @metadata = metadata end end # Base class which is used to implement cryptor that should be used with # CryptorProvider implementation for data encryption and decryption. class Cryptor # Identifier will be encoded into cryptor data header and passed along # with encrypted data and metadata. # # The identifier must be 4 bytes long. # # @return [String] Unique cryptor identifier. def identifier raise NotImplementedError, 'Subclass should provide "identifier" method implementation.' end # Encrypt provided data. # # @param data [String] Source data for encryption. # @return [EncryptedData, nil] Encrypted data or nil in case of # encryption error. def encrypt(data) raise NotImplementedError, 'Subclass should provide "encrypt" method implementation.' end # Decrypt provided data. # # @param data [EncryptedData] Encrypted data for decryption. # @return [String, nil] Decrypted data or nil in case of decryption # error. def decrypt(data) raise NotImplementedError, 'Subclass should provide "decrypt" method implementation.' end end end end