module Origami::Encryption::EncryptedDocument

Attributes

encryption_dict[W]
encryption_key[W]
stm_algo[W]
str_algo[W]

Private Instance Methods

physicalize(options = {}) click to toggle source
# File lib/origami/encryption.rb, line 329
def physicalize(options = {})

  def build(obj, revision, options) #:nodoc:
    if obj.is_a?(EncryptedObject) # already built
      if options[:decrypt] == true
        obj.pre_build
        obj.decrypt!
        obj.decrypted = false # makes it believe no encryption pass is required
        obj.post_build
      end

      return
    end
     
    if obj.is_a?(ObjectStream)
      obj.each do |subobj|
        build(subobj, revision, options)
      end
    end

    obj.pre_build

    case obj
    when String
      if not obj.equal?(@encryption_dict[:U]) and 
         not obj.equal?(@encryption_dict[:O]) and 
         not obj.equal?(@encryption_dict[:UE]) and 
         not obj.equal?(@encryption_dict[:OE]) and 
         not obj.equal?(@encryption_dict[:Perms]) and 
         not (obj.parent.is_a?(Signature::DigitalSignature) and obj.equal?(obj.parent[:Contents])) and
         not obj.indirect_parent.parent.is_a?(ObjectStream)
        
        obj.extend(EncryptedString)
        obj.decrypted = true
        obj.encryption_handler = @encryption_dict
        obj.encryption_key = @encryption_key
        obj.algorithm = @str_algo
      end

    when Stream
      return if obj.is_a?(XRefStream)
      return if obj.equal?(self.Catalog.Metadata) and not @encryption_dict.EncryptMetadata
      obj.extend(EncryptedStream)
      obj.decrypted = true
      obj.encryption_handler = @encryption_dict
      obj.encryption_key = @encryption_key
      obj.algorithm = @stm_algo

    when Dictionary, Array
      obj.map! do |subobj|
        if subobj.is_indirect?
          if get_object(subobj.reference)
            subobj.reference
          else
            ref = add_to_revision(subobj, revision)
            build(subobj, revision, options)
            ref
          end
        else
          subobj
        end
      end
      
      obj.each do |subobj|
        build(subobj, revision, options)
      end
    end

    obj.post_build
  end

  # stack up every root objects
  indirect_objects_by_rev.each do |obj, revision|
    build(obj, revision, options)
  end

  # remove encrypt dictionary if requested
  if options[:decrypt]
    delete_object(get_trailer_info[:Encrypt])
    get_trailer_info[:Encrypt] = nil
  end
  
  self
end