lib/hexapdf/encryption/security_handler.rb in hexapdf-0.20.2 vs lib/hexapdf/encryption/security_handler.rb in hexapdf-0.20.3

- old
+ new

@@ -247,10 +247,14 @@ # Creates a new SecurityHandler for the given document. def initialize(document) @document = document @encrypt_dict_hash = nil @encryption_details = {} + + @is_encrypt_dict = document.revisions.each.with_object({}) do |rev, hash| + hash[rev.trailer[:Encrypt]] = true + end end # Checks if the encryption key computed by this security handler is derived from the # document's encryption dictionary. def encryption_key_valid? @@ -260,10 +264,10 @@ # Decrypts the strings and the possibly attached stream of the given indirect object in # place. # # See: PDF1.7 s7.6.2 def decrypt(obj) - return obj if obj == document.trailer[:Encrypt] || obj.type == :XRef + return obj if @is_encrypt_dict[obj] || obj.type == :XRef key = object_key(obj.oid, obj.gen, string_algorithm) each_string_in_object(obj.value) do |str| next if str.empty? || (obj.type == :Sig && obj[:Contents].equal?(str)) str.replace(string_algorithm.decrypt(key, str))