module Origami::Encryption::EncryptedDocument

def build_object(object, revision, options)

def build_object(object, revision, options)
    if object.is_a?(EncryptedObject) and options[:decrypt]
        object.pre_build
        object.decrypt!
        object.decrypted = false # makes it believe no encryption pass is required
        object.post_build
        return
    end
    super
end

def decrypt_objects


For each object subject to encryption, convert it to an EncryptedObject and decrypt it if necessary.
def decrypt_objects
    each_encryptable_object do |object|
        case object
        when String
            object.extend(EncryptedString) unless object.is_a?(EncryptedString)
            object.decrypt!
        when Stream
            object.extend(EncryptedStream) unless object.is_a?(EncryptedStream)
        end
    end
end

def each_encryptable_object(&b)


Iterates over each encryptable objects in the document.
def each_encryptable_object(&b)
    # Metadata may not be encrypted depending on the security handler configuration.
    encrypt_metadata = (@encryption_handler.EncryptMetadata != false)
    metadata = self.Catalog.Metadata
    self.each_object(recursive: true)
        .lazy
        .select { |object|
            case object
            when Stream
                not object.is_a?(XRefStream) or (encrypt_metadata and object.equal?(metadata))
            when String
                not object.parent.equal?(@encryption_handler)
            end
        }
        .each(&b)
end

def encrypt_objects


For each object subject to encryption, convert it to an EncryptedObject and mark it as not encrypted yet.
def encrypt_objects
    each_encryptable_object do |object|
        case object
        when String
            unless object.is_a?(EncryptedString)
                object.extend(EncryptedString)
                object.decrypted = true
            end
        when Stream
            unless object.is_a?(EncryptedStream)
                object.extend(EncryptedStream)
                object.decrypted = true
            end
        end
    end
end

def encryption_cipher(name)

Get the encryption cipher from the crypt filter name.
def encryption_cipher(name)
    @encryption_handler.encryption_cipher(name)
end

def physicalize(options = {})

def physicalize(options = {})
    encrypt_objects
    super
    # remove encrypt dictionary if requested
    if options[:decrypt]
        delete_object(self.trailer[:Encrypt])
        self.trailer[:Encrypt] = nil
    end
    self
end

def stream_encryption_cipher

Get the default stream encryption cipher.
def stream_encryption_cipher
    @encryption_handler.stream_encryption_cipher
end

def string_encryption_cipher

Get the default string encryption cipher.
def string_encryption_cipher
    @encryption_handler.string_encryption_cipher
end