Class: PDF::Reader::StandardKeyBuilder
- Inherits:
-
Object
- Object
- PDF::Reader::StandardKeyBuilder
- 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
-
#initialize(opts = {}) ⇒ StandardKeyBuilder
constructor
: (?Hash[Symbol, untyped]) -> void.
-
#key(pass = "") ⇒ Object
Takes a string containing a user provided password.
Constructor Details
#initialize(opts = {}) ⇒ StandardKeyBuilder
: (?Hash[Symbol, untyped]) -> void
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pdf/reader/standard_key_builder.rb', line 30 def initialize(opts = {}) @key_length = opts[:key_length].to_i/8 #: Integer @revision = opts[:revision].to_i #: Integer @owner_key = opts[:owner_key] #: String? @user_key = opts[:user_key] #: String? @permissions = opts[:permissions].to_i #: Integer @encryptMeta = opts.fetch(:encrypted_metadata, true) #: bool @file_id = opts[:file_id] || "" #: String 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.
: (?String) -> String
53 54 55 56 57 58 59 |
# File 'lib/pdf/reader/standard_key_builder.rb', line 53 def key(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 |