Class: PDF::Writer::Object::Encryption

Inherits:
PDF::Writer::Object show all
Defined in:
lib/pdf/writer/object/encryption.rb

Overview

Encryption object

Constant Summary collapse

PAD =
[ 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 ].pack("C*")

Instance Attribute Summary

Attributes inherited from PDF::Writer::Object

#oid

Instance Method Summary collapse

Constructor Details

#initialize(parent, options) ⇒ Encryption

Returns a new instance of Encryption.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdf/writer/object/encryption.rb', line 18

def initialize(parent, options)
  super(parent)

  @parent.encrypt_obj = self

    # Figure out the additional parameters required.
  @owner  = "#{options[:owner_pass]}#{PAD}"[0...32]
  @user   = "#{options[:user_pass]}#{PAD}"[0...32]
  @perms  = options[:permissions]

  @parent.arc4.prepare(Digest::MD5.hexdigest(@owner)[0...5])

    # Get the 'O' value.
  @owner_info = ARC4.encrypt(@user)
    # Get the 'U' value.
  ukey = @user.dup
  ukey << @owner_info
  ukey << [ @perms, 0xFF, 0xFF, 0xFF ].pack("C*")
  ukey << @parent.file_identifier
  @parent.encryption_key = Digest::MD5.hexdigest(ukey)[0...5]

  @parent.arc4.prepare(@parent.encryption_key)

  @user_info = @parent.arc4.encrypt(PAD)
end

Instance Method Details

#to_sObject



44
45
46
47
48
49
50
51
52
# File 'lib/pdf/writer/object/encryption.rb', line 44

def to_s
  res = "\n#{@oid} 0 obj\n<<\n/Filter /Standard\n"
  res << "/V 1\n/R 2\n"
  res << "/O (#{PDF::Writer.escape(@owner_info)})\n"
  res << "/U (#{PDF::Writer.escape(@user_info)})\n"
  res << "/P #{(((@perms ^ 255) + 1) * -1)}\n"
  res << ">>\nendobj\n"
  res
end