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.

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
# 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 = document.resolve(ref)
  return nil unless encrypt

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

.lookup(filter_name) ⇒ Object



61
62
63
# File 'lib/pdfrb/encryption/security_handler.rb', line 61

def lookup(filter_name)
  registry[filter_name.to_s]
end

.register(filter_name, klass) ⇒ Object



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

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

.registryObject



53
54
55
# File 'lib/pdfrb/encryption/security_handler.rb', line 53

def registry
  @registry ||= {}
end

Instance Method Details

#decrypt(_bytes, _oid, _gen) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#encrypt(_bytes, _oid, _gen) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#encrypted?Boolean

Returns:

  • (Boolean)


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

def encrypted?
  !@encrypt_dict.nil?
end

#set_up_decryption(**_opts) ⇒ Object

Subclasses override.

Raises:

  • (NotImplementedError)


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

def set_up_decryption(**_opts)
  raise NotImplementedError
end