Completed
Push — master ( bc5c42...47bad9 )
by Ben
02:53
created

Pdf16Encryption::encrypt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 11
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 22
rs 9.2
cc 3
eloc 14
nc 2
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
class Pdf16Encryption extends Pdf14Encryption
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function encrypt($plaintext, $objectNumber, $generationNumber)
18
    {
19 View Code Duplication
        if (function_exists('random_bytes')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
            // As of PHP 7
21
            $initializationVector = random_bytes(16);
22
        } else {
23
            $initializationVector = '';
24
            mt_srand();
25
26
            for ($i = 0; $i < 16; ++$i) {
27
                $initializationVector .= chr(mt_rand(0, 255));
28
            }
29
        }
30
31
        return $initializationVector . openssl_encrypt(
32
            $plaintext,
33
            'aes-128-cbc',
34
            $this->computeIndividualEncryptionKey($objectNumber, $generationNumber) . "\x73\x41\x6c\x54",
35
            '',
36
            $initializationVector
37
        );
38
    }
39
}
40