Passed
Push — main ( 9ec2a5...ad6224 )
by Sugeng
02:44
created

EncrypterTrait::initEncrypter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace diecoding\flysystem\traits;
4
5
use yii\base\InvalidConfigException;
6
use yii\helpers\StringHelper;
7
8
/**
9
 * Trait EncrypterTrait for Model
10
 * 
11
 * @link      https://sugengsulistiyawan.my.id/
12
 * @author    Sugeng Sulistiyawan <[email protected]>
13
 * @copyright Copyright (c) 2023
14
 */
15
trait EncrypterTrait
16
{
17
    /**
18
     * @var string
19
     */
20
    private $_cipherAlgo;
21
22
    /**
23
     * @var string
24
     */
25
    private $_passphrase;
26
27
    /**
28
     * @var string
29
     */
30
    private $_iv;
31
32
    /**
33
     * Init Encrypter
34
     * 
35
     * @param string $passphrase
36
     * @param string $iv
37
     * 
38
     * @return void
39
     * @throws InvalidConfigException
40
     */
41
    public function initEncrypter($passphrase, $iv, $cipherAlgo = 'aes-128-cbc')
42
    {
43
        $this->_cipherAlgo = $cipherAlgo;
44
45
        $this->normalizePassphrase($passphrase);
46
        $this->normalizeIv($iv);
47
    }
48
49
    /**
50
     * Encrypts a string.
51
     * 
52
     * @param string $string the string to encrypt
53
     * @return string the encrypted string
54
     */
55
    public function encrypt($string)
56
    {
57
        $encryptedString = openssl_encrypt($string, $this->_cipherAlgo, $this->_passphrase, OPENSSL_RAW_DATA, $this->_iv);
58
        $encryptedString = StringHelper::base64UrlEncode($encryptedString);
59
60
        return $encryptedString;
61
    }
62
63
    /**
64
     * Decrypts a string. 
65
     * False is returned in case it was not possible to decrypt it.
66
     * 
67
     * @param string $string the string to decrypt
68
     * @return string the decrypted string
69
     */
70
    public function decrypt($string)
71
    {
72
        $decodedString = StringHelper::base64UrlDecode($string);
73
        $decodedString = openssl_decrypt($decodedString, $this->_cipherAlgo, $this->_passphrase, OPENSSL_RAW_DATA, $this->_iv);
74
75
        return $decodedString;
76
    }
77
78
    /**
79
     * @param string $passphrase
80
     * @param int $minPassphraseLength
81
     * 
82
     * @return void
83
     * @throws InvalidConfigException
84
     */
85
    private function normalizePassphrase($passphrase, $minPassphraseLength = 32)
86
    {
87
        $this->_passphrase = $passphrase;
88
        if (empty($this->_passphrase)) {
89
            throw new InvalidConfigException('The "passphrase" property must be set.');
90
        }
91
92
        $passphraseLength = strlen($this->_passphrase);
93
        if ($passphraseLength < $minPassphraseLength) {
94
            $this->_passphrase = str_repeat($this->_passphrase, (int) ceil($minPassphraseLength / $passphraseLength));
95
        }
96
    }
97
98
    /**
99
     * @param string $iv
100
     * 
101
     * @return void
102
     * @throws InvalidConfigException
103
     */
104
    private function normalizeIv($iv)
105
    {
106
        $this->_iv = $iv;
107
        if (empty($this->_iv)) {
108
            throw new InvalidConfigException('The "iv" property must be set.');
109
        }
110
111
        $ivLength     = strlen($this->_iv);
112
        $mustIvLength = openssl_cipher_iv_length($this->_cipherAlgo);
113
        if ($ivLength < $mustIvLength) {
114
            $this->_iv = str_repeat($this->_iv, (int) ceil($mustIvLength / $ivLength));
115
        }
116
117
        $this->_iv = substr($this->_iv, 0, $mustIvLength);
118
    }
119
}
120