Passed
Push — main ( e05860...0ac74e )
by Sugeng
03:15 queued 13s
created

EncrypterTrait::normalizePassphrase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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