Pdf16Encryption::encrypt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
rs 9.4286
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben Scholzen (DASPRiD)
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf\Encryption;
11
12
use Bacon\Pdf\Writer\ObjectWriter;
13
14
/**
15
 * Encryption for handling PDF version 1.6 and above.
16
 */
17
class Pdf16Encryption extends Pdf14Encryption
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected function writeAdditionalEncryptDictionaryEntries(ObjectWriter $objectWriter)
23
    {
24
        parent::writeAdditionalEncryptDictionaryEntries($objectWriter);
25
26
        $objectWriter->writeName('CF');
27
        $objectWriter->startDictionary();
28
29
        $objectWriter->writeName('StdCF');
30
        $objectWriter->startDictionary();
31
32
        $objectWriter->writeName('Type');
33
        $objectWriter->writeName('CryptFilter');
34
35
        $objectWriter->writeName('CFM');
36
        $objectWriter->writeName('AESV2');
37
38
        $objectWriter->writeName('Length');
39
        $objectWriter->writeNumber(128);
40
41
        $objectWriter->endDictionary();
42
        $objectWriter->endDictionary();
43
44
        $objectWriter->writeName('StrF');
45
        $objectWriter->writeName('StdCF');
46
47
        $objectWriter->writeName('StmF');
48
        $objectWriter->writeName('StdCF');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function encrypt($plaintext, $objectNumber, $generationNumber)
55
    {
56
        $initializationVector = openssl_random_pseudo_bytes(16);
57
58
        return $initializationVector . openssl_encrypt(
59
            $plaintext,
60
            'aes-128-cbc',
61
            $this->computeIndividualEncryptionKey($objectNumber, $generationNumber) . "\x73\x41\x6c\x54",
62
            true,
63
            $initializationVector
64
        );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function getRevision()
71
    {
72
        return 4;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function getAlgorithm()
79
    {
80
        return 4;
81
    }
82
}
83