Class: PDF::Reader::StandardSecurityHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/standard_security_handler.rb

Overview

class creates interface to encrypt dictionary for use in Decrypt

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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ StandardSecurityHandler

Returns a new instance of StandardSecurityHandler.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pdf/reader/standard_security_handler.rb', line 49

def initialize(opts = {})
  @key_length    = opts[:key_length].to_i/8
  @revision      = opts[:revision].to_i
  @owner_key     = opts[:owner_key]
  @user_key      = opts[:user_key]
  @permissions   = opts[:permissions].to_i
  @encryptMeta   = opts.fetch(:encrypted_metadata, true)
  @file_id       = opts[:file_id] || ""
  @encrypt_key   = build_standard_key(opts[:password] || "")
  @cfm           = opts[:cfm]

  if @key_length != 5 && @key_length != 16
    msg = "StandardSecurityHandler only supports 40 and 128 bit\
           encryption (#{@key_length * 8}bit)"
    raise ArgumentError, msg
  end
end

Instance Attribute Details

#encrypt_keyObject (readonly)

Returns the value of attribute encrypt_key.



46
47
48
# File 'lib/pdf/reader/standard_security_handler.rb', line 46

def encrypt_key
  @encrypt_key
end

#file_idObject (readonly)

Returns the value of attribute file_id.



47
48
49
# File 'lib/pdf/reader/standard_security_handler.rb', line 47

def file_id
  @file_id
end

#key_lengthObject (readonly)

Returns the value of attribute key_length.



46
47
48
# File 'lib/pdf/reader/standard_security_handler.rb', line 46

def key_length
  @key_length
end

#owner_keyObject (readonly)

Returns the value of attribute owner_key.



47
48
49
# File 'lib/pdf/reader/standard_security_handler.rb', line 47

def owner_key
  @owner_key
end

#passwordObject (readonly)

Returns the value of attribute password.



47
48
49
# File 'lib/pdf/reader/standard_security_handler.rb', line 47

def password
  @password
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



47
48
49
# File 'lib/pdf/reader/standard_security_handler.rb', line 47

def permissions
  @permissions
end

#revisionObject (readonly)

Returns the value of attribute revision.



46
47
48
# File 'lib/pdf/reader/standard_security_handler.rb', line 46

def revision
  @revision
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



47
48
49
# File 'lib/pdf/reader/standard_security_handler.rb', line 47

def user_key
  @user_key
end

Class Method Details

.supports?(encrypt) ⇒ Boolean

This handler supports all encryption that follows upto PDF 1.5 spec (revision 4)

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/pdf/reader/standard_security_handler.rb', line 68

def self.supports?(encrypt)
  return false if encrypt.nil?

  filter = encrypt.fetch(:Filter, :Standard)
  version = encrypt.fetch(:V, 0)
  algorithm = encrypt.fetch(:CF, {}).fetch(encrypt[:StmF], {}).fetch(:CFM, nil)
  (filter == :Standard) && (encrypt[:StmF] == encrypt[:StrF]) &&
    (version <= 3 || (version == 4 && ((algorithm == :V2) || (algorithm == :AESV2))))
end

Instance Method Details

#decrypt(buf, ref) ⇒ Object

7.6.2 General Encryption Algorithm

Algorithm 1: Encryption of data using the RC4 or AES algorithms

used to decrypt RC4/AES encrypted PDF streams (buf)

buf - a string to decrypt ref - a PDF::Reader::Reference for the object to decrypt



87
88
89
90
91
92
93
94
# File 'lib/pdf/reader/standard_security_handler.rb', line 87

def decrypt( buf, ref )
  case @cfm
    when :AESV2
      decrypt_aes128(buf, ref)
    else
      decrypt_rc4(buf, ref)
  end
end