Class: PDF::Reader::StandardSecurityHandlerV5

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/standard_security_handler_v5.rb

Overview

class creates interface to encrypt dictionary for use in Decrypt

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StandardSecurityHandlerV5

Returns a new instance of StandardSecurityHandlerV5.



12
13
14
15
16
17
18
19
# File 'lib/pdf/reader/standard_security_handler_v5.rb', line 12

def initialize(opts = {})
  @key_length   = 256
  @O            = opts[:O]   # hash(32B) + validation salt(8B) + key salt(8B)
  @U            = opts[:U]   # hash(32B) + validation salt(8B) + key salt(8B)
  @OE           = opts[:OE]  # decryption key, encrypted w/ owner password
  @UE           = opts[:UE]  # decryption key, encrypted w/ user password
  @encrypt_key  = build_standard_key(opts[:password] || '')
end

Instance Attribute Details

#encrypt_keyObject (readonly)

Returns the value of attribute encrypt_key.



10
11
12
# File 'lib/pdf/reader/standard_security_handler_v5.rb', line 10

def encrypt_key
  @encrypt_key
end

#key_lengthObject (readonly)

Returns the value of attribute key_length.



10
11
12
# File 'lib/pdf/reader/standard_security_handler_v5.rb', line 10

def key_length
  @key_length
end

Class Method Details

.supports?(encrypt) ⇒ Boolean

This handler supports AES-256 encryption defined in PDF 1.7 Extension Level 3

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
# File 'lib/pdf/reader/standard_security_handler_v5.rb', line 22

def self.supports?(encrypt)
  return false if encrypt.nil?

  filter = encrypt.fetch(:Filter, :Standard)
  version = encrypt.fetch(:V, 0)
  revision = encrypt.fetch(:R, 0)
  algorithm = encrypt.fetch(:CF, {}).fetch(encrypt[:StmF], {}).fetch(:CFM, nil)
  (filter == :Standard) && (encrypt[:StmF] == encrypt[:StrF]) &&
      ((version == 5) && (revision == 5) && (algorithm == :AESV3))
end

Instance Method Details

#decrypt(buf, ref) ⇒ Object

7.6.2 General Encryption Algorithm

Algorithm 1: Encryption of data using the RC4 or AES algorithms

used to decrypt RC4/AES encrypted PDF streams (buf)

buf - a string to decrypt ref - a PDF::Reader::Reference for the object to decrypt



42
43
44
45
46
47
48
# File 'lib/pdf/reader/standard_security_handler_v5.rb', line 42

def decrypt( buf, ref )
  cipher = OpenSSL::Cipher.new("AES-#{@key_length}-CBC")
  cipher.decrypt
  cipher.key = @encrypt_key.dup
  cipher.iv = buf[0..15]
  cipher.update(buf[16..-1]) + cipher.final
end