Class: Pdfrb::Encryption::SecurityHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/encryption/security_handler.rb

Overview

Security handler base class. Subclasses implement decrypt, encrypt, set_up_decryption, set_up_encryption. Dispatch is via /Filter on the trailer's /Encrypt dict.

Direct Known Subclasses

StandardSecurityHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ SecurityHandler

Returns a new instance of SecurityHandler.



11
12
13
14
# File 'lib/pdfrb/encryption/security_handler.rb', line 11

def initialize(document)
  @document = document
  @encrypt_dict = nil
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



9
10
11
# File 'lib/pdfrb/encryption/security_handler.rb', line 9

def document
  @document
end

#encrypt_dictObject (readonly)

Returns the value of attribute encrypt_dict.



9
10
11
# File 'lib/pdfrb/encryption/security_handler.rb', line 9

def encrypt_dict
  @encrypt_dict
end

Class Method Details

.for(document, decryption_opts: {}) ⇒ Object

Dispatch on the trailer's /Encrypt /Filter.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdfrb/encryption/security_handler.rb', line 17

def self.for(document, decryption_opts: {})
  trailer = document.trailer
  return nil unless trailer

  ref = trailer[:Encrypt]
  return nil unless ref

  encrypt = ref.is_a?(Pdfrb::Model::Reference) ?
              document.object(ref) : ref
  return nil unless encrypt

  filter = encrypt[:Filter] || :Standard
  klass = HANDLERS[filter.to_s] ||
          raise(Pdfrb::EncryptionError, "unsupported /Filter #{filter.inspect}")
  handler = klass.new(document)
  handler.set_up_decryption(**decryption_opts)
  handler
end

.register(filter_name, klass) ⇒ Object



59
60
61
# File 'lib/pdfrb/encryption/security_handler.rb', line 59

def register(filter_name, klass)
  registry[filter_name.to_s] = klass
end

.registryObject



57
# File 'lib/pdfrb/encryption/security_handler.rb', line 57

def registry; @registry ||= {}; end

Instance Method Details

#decrypt(_bytes, _oid, _gen) ⇒ Object

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/pdfrb/encryption/security_handler.rb', line 41

def decrypt(_bytes, _oid, _gen)
  raise NotImplementedError
end

#encrypt(_bytes, _oid, _gen) ⇒ Object

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/pdfrb/encryption/security_handler.rb', line 45

def encrypt(_bytes, _oid, _gen)
  raise NotImplementedError
end

#encrypted?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/pdfrb/encryption/security_handler.rb', line 49

def encrypted?
  !@encrypt_dict.nil?
end

#set_up_decryption(**_opts) ⇒ Object

Subclasses override.

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/pdfrb/encryption/security_handler.rb', line 37

def set_up_decryption(**_opts)
  raise NotImplementedError
end