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

: (?Hash[Symbol, String]) -> void



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

def initialize(opts = {})
  @key_length   = 256 #: Integer

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

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

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

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

: (String) -> String



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

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