Module: Pdfrb::Encryption

Defined in:
lib/pdfrb/encryption.rb,
lib/pdfrb/encryption/aes.rb,
lib/pdfrb/encryption/rc4.rb,
lib/pdfrb/encryption/identity.rb,
lib/pdfrb/encryption/v5_writer.rb,
lib/pdfrb/encryption/value_strings.rb,
lib/pdfrb/encryption/security_handler.rb,
lib/pdfrb/encryption/password_verification.rb,
lib/pdfrb/encryption/standard_security_handler.rb,
lib/pdfrb/encryption/public_key_security_handler.rb

Overview

Encryption layer (s7.6). Security handlers own per-document key derivation and per-object (de)cryption. The Standard handler covers V1..V6 / R2..R6 (RC4 40-bit through AES-256).

The Crypt stream filter routes through the active handler; the Serializer accepts an encrypter slot set by the handler.

Defined Under Namespace

Modules: Identity, PasswordVerification, V5Writer, ValueStrings Classes: AES, PublicKeySecurityHandler, RC4, RC4Impl, SecurityHandler, StandardSecurityHandler

Constant Summary collapse

V4_AESV2_FILTERS =

Crypt-filter entries a V4 /Encrypt dict must carry when the cipher is AES-128: absent StmF/StrF default to Identity (s7.6.3.2 Table 21), leaving the ciphertext unreadable.

{
  CF: { StdCF: { CFM: :AESV2, Length: 16 } },
  StmF: :StdCF,
  StrF: :StdCF,
}.freeze

Class Method Summary collapse

Class Method Details

.exempt_object?(document, obj) ⇒ Boolean

Objects whose bytes are never encrypted (both write and read directions consult this): the /Encrypt dictionary itself (s7.6.3 — its values carry the password hashes) and cross-reference streams (always cleartext).

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/pdfrb/encryption.rb', line 54

def exempt_object?(document, obj)
  encrypt_oid = (document.trailer || {})[:Encrypt]&.oid
  obj.oid == encrypt_oid ||
    (obj.is_a?(Pdfrb::Model::Cos::Stream) && obj.value[:Type] == :XRef)
end

.handler_for(filter_name) ⇒ Object

Look up a registered security-handler subclass by /Filter name.



27
28
29
# File 'lib/pdfrb/encryption.rb', line 27

def handler_for(filter_name)
  SecurityHandler.lookup(filter_name)
end

.handler_for_document(document, **opts) ⇒ Object

Build a security handler for document by inspecting its /Encrypt. Returns nil if the document is not encrypted.



33
34
35
# File 'lib/pdfrb/encryption.rb', line 33

def handler_for_document(document, **opts)
  SecurityHandler.for(document, decryption_opts: opts)
end

.v4_crypt_filters(v) ⇒ Object



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

def v4_crypt_filters(v)
  v == 4 ? V4_AESV2_FILTERS : {}
end