Test Failed
Push — main ( 1f6f9b...f1a6e0 )
by Sugeng
03:22
created

EncrypterTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 2
b 0
f 0
dl 0
loc 81
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 6 1
A normalizeIv() 0 9 2
A setEncrypter() 0 14 3
A decrypt() 0 6 1
1
<?php
2
3
namespace diecoding\flysystem\traits;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\helpers\StringHelper;
8
9
/**
10
 * Trait EncrypterTrait for Model
11
 * 
12
 * @package diecoding\flysystem\traits
13
 * 
14
 * @link      https://sugengsulistiyawan.my.id/
15
 * @author    Sugeng Sulistiyawan <[email protected]>
16
 * @copyright Copyright (c) 2023
17
 */
18
trait EncrypterTrait
19
{
20
    /**
21
     * @var string
22
     */
23
    private $_cipherAlgo;
24
25
    /**
26
     * @var string
27
     */
28
    private $_passphrase;
29
30
    /**
31
     * @var string
32
     */
33
    private $_iv;
34
35
    /**
36
     * Set Encrypter
37
     * 
38
     * @return void
39
     * @throws InvalidConfigException
40
     */
41
    public function setEncrypter($passphrase, $iv, $cipherAlgo = 'aes-128-cbc')
42
    {
43
        $this->_passphrase = $passphrase;
44
        $this->_iv         = $iv;
45
        $this->_cipherAlgo = $cipherAlgo;
46
47
        if (empty($this->_passphrase)) {
48
            throw new InvalidConfigException('The "passphrase" property must be set.');
49
        }
50
        if (empty($this->_iv)) {
51
            throw new InvalidConfigException('The "iv" property must be set.');
52
        }
53
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
     * @return void
88
     * @throws InvalidConfigException
89
     */
90
    private function normalizeIv()
91
    {
92
        $ivLength     = strlen($this->_iv);
93
        $mustIvLength = openssl_cipher_iv_length($this->_cipherAlgo);
94
        if ($ivLength < $mustIvLength) {
95
            $this->_iv = str_repeat($this->_iv, (int) ceil($mustIvLength / $ivLength));
96
        }
97
98
        $this->_iv = substr($this->_iv, 0, $mustIvLength);
99
    }
100
}
101