lib/hexapdf/encryption/security_handler.rb in hexapdf-0.6.0 vs lib/hexapdf/encryption/security_handler.rb in hexapdf-0.7.0
- old
+ new
@@ -1,12 +1,12 @@
-# -*- encoding: utf-8 -*-
+# -*- encoding: utf-8; frozen_string_literal: true -*-
#
#--
# This file is part of HexaPDF.
#
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
-# Copyright (C) 2014-2017 Thomas Leitner
+# Copyright (C) 2014-2018 Thomas Leitner
#
# HexaPDF is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3 as
# published by the Free Software Foundation with the addition of the
# following permission added to Section 15 as permitted in Section 7(a):
@@ -142,16 +142,16 @@
attr_reader :algorithm
# Creates a new encrypted stream data object by utilizing the given stream data object as
# template. The arguments +key+ and +algorithm+ are used for decrypting purposes.
def initialize(obj, key, algorithm)
- obj.instance_variables.each {|v| instance_variable_set(v, obj.instance_variable_get(v))}
+ obj.instance_variables.each {|v| instance_variable_set(v, obj.instance_variable_get(v)) }
@key = key
@algorithm = algorithm
end
- alias :undecrypted_fiber :fiber
+ alias undecrypted_fiber fiber
# Returns a fiber like HexaPDF::StreamData#fiber, but one wrapped in a decrypting fiber.
def fiber(*args)
@algorithm.decryption_fiber(@key, super(*args))
end
@@ -213,11 +213,10 @@
end
handler.freeze
end
-
# A hash containing information about the used encryption. This information is only
# available once the security handler has been set up for decryption or encryption.
#
# Available keys:
#
@@ -308,25 +307,25 @@
#
# algorithm::
# The encryption algorithm. Possible values are :arc4 for ARC4 encryption with key lengths
# of 40 to 128 bit or :aes for AES encryption with key lengths of 128 or 256 bit.
#
- # force_V4::
+ # force_v4::
# Forces the use of protocol version 4 when key_length=128 and algorithm=:arc4.
#
# See: PDF1.7 s7.6.1, PDF2.0 s7.6.1
- def set_up_encryption(key_length: 128, algorithm: :aes, force_V4: false, **options)
+ def set_up_encryption(key_length: 128, algorithm: :aes, force_v4: false, **options)
@dict = document.wrap({}, type: encryption_dictionary_class)
dict[:V] =
case key_length
when 40
1
when 48, 56, 64, 72, 80, 88, 96, 104, 112, 120
2
when 128
- (algorithm == :aes || force_V4 ? 4 : 2)
+ (algorithm == :aes || force_v4 ? 4 : 2)
when 256
5
else
raise(HexaPDF::UnsupportedEncryptionError,
"Invalid key length #{key_length} specified")
@@ -471,12 +470,12 @@
# See: PDF1.7 s7.6.2 (algorithm 1), PDF2.0 s7.6.2.2 (algorithm 1.A)
def object_key(oid, gen, algorithm)
key = encryption_key
return key if dict[:V] == 5
- key += [oid, gen].pack('VXv'.freeze)
- key << "sAlT".freeze if algorithm.ancestors.include?(AES)
+ key += [oid, gen].pack('VXv')
+ key << "sAlT" if algorithm.ancestors.include?(AES)
n_plus_5 = key_length + 5
Digest::MD5.digest(key)[0, (n_plus_5 > 16 ? 16 : n_plus_5)]
end
# Returns the length of the encryption key in bytes based on the security handlers version.
@@ -506,12 +505,12 @@
# Note: Decryption happens directly after parsing and loading an object, before it can be
# touched by anthing else. Therefore we only have to contend with the basic data structures.
def each_string_in_object(obj, &block) # :yields: str
case obj
when Hash
- obj.each_value {|val| each_string_in_object(val, &block)}
+ obj.each_value {|val| each_string_in_object(val, &block) }
when Array
- obj.each {|inner_o| each_string_in_object(inner_o, &block)}
+ obj.each {|inner_o| each_string_in_object(inner_o, &block) }
when String
yield(obj)
end
end