|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* OpensslStack.php. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7 |
|
9
|
|
|
* |
|
10
|
|
|
* @category Dcrypt |
|
11
|
|
|
* |
|
12
|
|
|
* @author Michael Meyer (mmeyer2k) <[email protected]> |
|
13
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
14
|
|
|
* |
|
15
|
|
|
* @link https://github.com/mmeyer2k/dcrypt |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Dcrypt; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A factory class to build and use custom encryption stacks. |
|
22
|
|
|
* |
|
23
|
|
|
* @category Dcrypt |
|
24
|
|
|
* |
|
25
|
|
|
* @author Michael Meyer (mmeyer2k) <[email protected]> |
|
26
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
27
|
|
|
* |
|
28
|
|
|
* @link https://github.com/mmeyer2k/dcrypt |
|
29
|
|
|
*/ |
|
30
|
|
|
class OpensslStack |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Stack of cipher/algo combos. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $_stack = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* High entropy key. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $_key; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* OpensslStack constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key Password or key |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function __construct(string $key) |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->_key = $key; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Add a new cipher/algo combo to the execution stack. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $cipher Cipher mode to use |
|
60
|
|
|
* @param string $algo Hashing algo to use |
|
61
|
|
|
* |
|
62
|
|
|
* @return OpensslStack |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function add(string $cipher, string $algo): self |
|
65
|
|
|
{ |
|
66
|
2 |
|
$this->_stack[] = [$cipher, $algo]; |
|
67
|
|
|
|
|
68
|
2 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Encrypt data using custom stack. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $data Data to encrypt |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function encrypt(string $data): string |
|
79
|
|
|
{ |
|
80
|
2 |
|
foreach ($this->_stack as $s) { |
|
81
|
2 |
|
$data = OpensslStatic::encrypt($data, $this->_key, $s[0], $s[1]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
return $data; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Decrypt data using custom stack. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $data Data to decrypt |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function decrypt(string $data): string |
|
95
|
|
|
{ |
|
96
|
2 |
|
foreach (\array_reverse($this->_stack) as $s) { |
|
97
|
2 |
|
$data = OpensslStatic::decrypt($data, $this->_key, $s[0], $s[1]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
return $data; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|