Passed
Branch master (bfd3f5)
by Wilder
01:18
created

AES256EncryptionStrategy::decrypt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.9332
1
<?php
2
3
namespace ElePHPant\Cookie\Strategies\Encryption;
4
5
use ElePHPant\Cookie\Exceptions\InvalidParamException;
6
7
/**
8
 * Class AES256EncryptionStrategy
9
 *
10
 * Please report bugs on https://github.com/wilderamorim/cookie/issues
11
 *
12
 * @author Wilder Amorim <https://github.com/wilderamorim>
13
 * @link https://www.linkedin.com/in/wilderamorim/
14
 */
15
class AES256EncryptionStrategy implements EncryptionStrategyInterface
16
{
17
    /** @var string The encryption key used for AES-256 encryption. */
18
    private string $encryptKey;
19
20
    /**
21
     * Create a new instance of AES256EncryptionStrategy.
22
     *
23
     * @param array $configs The configuration array.
24
     */
25
    public function __construct(array $configs)
26
    {
27
        $this->boot($configs);
28
    }
29
30
    /**
31
     * Encrypt a value using AES-256 encryption.
32
     *
33
     * @param string $value The value to encrypt.
34
     * @return string       The encrypted value.
35
     */
36
    public function encrypt(string $value): string
37
    {
38
        $iv = random_bytes(openssl_cipher_iv_length('aes-256-cbc'));
39
        $encrypted = openssl_encrypt($value, 'aes-256-cbc', $this->encryptKey, OPENSSL_RAW_DATA, $iv);
40
        return base64_encode($iv . $encrypted);
41
    }
42
43
    /**
44
     * Decrypt an encrypted value using AES-256 encryption.
45
     *
46
     * @param string $encryptedValue The encrypted value to decrypt.
47
     * @return string|null           The decrypted value, or null if decryption fails.
48
     */
49
    public function decrypt(string $encryptedValue): ?string
50
    {
51
        $encryptedValue = base64_decode($encryptedValue, true);
52
        if ($encryptedValue === false) {
53
            return null;
54
        }
55
56
        $ivLength = openssl_cipher_iv_length('aes-256-cbc');
57
        $iv = substr($encryptedValue, 0, $ivLength);
58
        $encrypted = substr($encryptedValue, $ivLength);
59
60
        $decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', $this->encryptKey, OPENSSL_RAW_DATA, $iv);
61
        if ($decrypted === false) {
62
            return null;
63
        }
64
65
        return $decrypted;
66
    }
67
68
    /**
69
     * Boot the encryption strategy by setting the encryption key.
70
     *
71
     * @param array $configs The configuration array.
72
     * @throws InvalidParamException If the encryption key is missing in the configuration.
73
     */
74
    public function boot(array $configs): void
75
    {
76
        if (!in_array('encrypt_key', array_keys($configs))) {
77
            throw new InvalidParamException('Encryption key is missing in params.');
78
        }
79
80
        $this->encryptKey = $configs['encrypt_key'];
81
    }
82
}
83