Class: PDF::Reader::StandardKeyBuilder

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

Overview

Processes the Encrypt dict from an encrypted PDF and a user provided password and returns a key that can decrypt the file.

This can generate a key compatible with the following standard encryption algorithms:

  • Version 1-3, all variants

  • Version 4, V2 (RC4) and AESV2

Constant Summary collapse

PassPadBytes =

7.6.3.3 Encryption Key Algorithm (pp61)

needs a document’s user password to build a key for decrypting an encrypted PDF document

[ 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a ]

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StandardKeyBuilder

Returns a new instance of StandardKeyBuilder.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdf/reader/standard_key_builder.rb', line 28

def initialize(opts = {})
  @key_length    = opts[:key_length].to_i/8
  @revision      = opts[:revision].to_i
  @owner_key     = opts[:owner_key]
  @user_key      = opts[:user_key]
  @permissions   = opts[:permissions].to_i
  @encryptMeta   = opts.fetch(:encrypted_metadata, true)
  @file_id       = opts[:file_id] || ""

  if @key_length != 5 && @key_length != 16
    msg = "StandardKeyBuilder only supports 40 and 128 bit\
           encryption (#{@key_length * 8}bit)"
    raise UnsupportedFeatureError, msg
  end
end

Instance Method Details

#key(pass) ⇒ Object

Takes a string containing a user provided password.

If the password matches the file, then a string containing a key suitable for decrypting the file will be returned. If the password doesn’t match the file, and exception will be raised.



50
51
52
53
54
55
56
57
# File 'lib/pdf/reader/standard_key_builder.rb', line 50

def key(pass)
  pass ||= ""
  encrypt_key   = auth_owner_pass(pass)
  encrypt_key ||= auth_user_pass(pass)

  raise PDF::Reader::EncryptedPDFError, "Invalid password (#{pass})" if encrypt_key.nil?
  encrypt_key
end