1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.5 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Encryption; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Encryption\Adapters\AsymmetricEncryptionAdapter; |
18
|
|
|
use Quantum\Libraries\Encryption\Contracts\EncryptionInterface; |
19
|
|
|
use Quantum\Libraries\Encryption\Exceptions\CryptorException; |
20
|
|
|
use Quantum\Exceptions\BaseException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Cryptor |
24
|
|
|
* @package Quantum\Libraries\Encryption |
25
|
|
|
* @method string encrypt(string $plain) |
26
|
|
|
* @method string decrypt(string $encrypted) |
27
|
|
|
*/ |
28
|
|
|
class Cryptor |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Symmetric |
33
|
|
|
*/ |
34
|
|
|
const SYMMETRIC = 'symmetric'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Asymmetric |
38
|
|
|
*/ |
39
|
|
|
const ASYMMETRIC = 'asymmetric'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var EncryptionInterface |
43
|
|
|
*/ |
44
|
|
|
private $adapter; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param EncryptionInterface $adapter |
48
|
|
|
*/ |
49
|
|
|
public function __construct(EncryptionInterface $adapter) |
50
|
|
|
{ |
51
|
|
|
$this->adapter = $adapter; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return EncryptionInterface |
56
|
|
|
*/ |
57
|
|
|
public function getAdapter(): EncryptionInterface |
58
|
|
|
{ |
59
|
|
|
return $this->adapter; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function isAsymmetric(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->adapter instanceof AsymmetricEncryptionAdapter; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $method |
72
|
|
|
* @param array|null $arguments |
73
|
|
|
* @return mixed |
74
|
|
|
* @throws BaseException |
75
|
|
|
*/ |
76
|
|
|
public function __call(string $method, ?array $arguments) |
77
|
|
|
{ |
78
|
|
|
if (!method_exists($this->adapter, $method)) { |
79
|
|
|
throw CryptorException::methodNotSupported($method, get_class($this->adapter)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->adapter->$method(...$arguments); |
83
|
|
|
} |
84
|
|
|
} |