OpenSslStrategy::createIv()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * File OpenSslStrategy.php
4
 */
5
6
namespace Tebru\AesEncryption\Strategy;
7
8
use Tebru\AesEncryption\Enum\AesEnum;
9
10
/**
11
 * Class OpenSslStrategy
12
 *
13
 * @author Nate Brunette <[email protected]>
14
 */
15
class OpenSslStrategy extends AesEncryptionStrategy
16
{
17
    /**
18
     * Create an initialization vector
19
     *
20
     * @return string
21
     */
22
    public function createIv()
23
    {
24
        return openssl_random_pseudo_bytes($this->getIvSize());
25
    }
26
27
    /**
28
     * Get the size of the IV
29
     *
30
     * @return int
31
     */
32
    public function getIvSize()
33
    {
34
        return openssl_cipher_iv_length($this->getEncryptionMethod());
35
    }
36
37
    /**
38
     * Encrypt data
39
     *
40
     * @param mixed $data
41
     * @param string $iv
42
     * @return mixed
43
     */
44
    public function encryptData($data, $iv)
45
    {
46
        return openssl_encrypt($data, $this->getEncryptionMethod(), $this->getKey(), true, $iv);
47
    }
48
49
    /**
50
     * Decrypt data
51
     *
52
     * @param $data
53
     * @param $iv
54
     * @return mixed
55
     */
56
    public function decryptData($data, $iv)
57
    {
58
        return openssl_decrypt($data, $this->getEncryptionMethod(), $this->getKey(), true, $iv);
59
    }
60
61
    /**
62
     * Get the openssl formatted encryption method
63
     *
64
     * @return string
65
     */
66
    private function getEncryptionMethod()
67
    {
68
        $keySize = AesEnum::getKeySize($this->getMethod()) * 8;
69
70
        return 'aes-' . $keySize . '-' . self::ENCRYPTION_MODE;
71
    }
72
}
73