Class: PDF::Reader::KeyBuilderV5

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/key_builder_v5.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 decryption key compatible with the following standard encryption algorithms:

  • Version 5 (AESV3)

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ KeyBuilderV5

Returns a new instance of KeyBuilderV5.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdf/reader/key_builder_v5.rb', line 19

def initialize(opts = {})
  @key_length   = 256

  # hash(32B) + validation salt(8B) + key salt(8B)
  @owner_key    = opts[:owner_key] || ""

  # hash(32B) + validation salt(8B) + key salt(8B)
  @user_key     = opts[:user_key] || ""

  # decryption key, encrypted w/ owner password
  @owner_encryption_key = opts[:owner_encryption_key] || ""

  # decryption key, encrypted w/ user password
  @user_encryption_key  = opts[:user_encryption_key] || ""
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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pdf/reader/key_builder_v5.rb', line 41

def key(pass)
  pass = pass.byteslice(0...127).to_s   # UTF-8 encoded password. first 127 bytes

  encrypt_key   = auth_owner_pass(pass)
  encrypt_key ||= auth_user_pass(pass)
  encrypt_key ||= auth_owner_pass_r6(pass)
  encrypt_key ||= auth_user_pass_r6(pass)

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