Repository navigation
Expand file tree
/
Copy pathEncryption.php
More file actions
271 lines (230 loc) · 6.78 KB
/
Copy pathEncryption.php
File metadata and controls
271 lines (230 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
declare(strict_types=1);
namespace Leevel\Encryption;
/**
* 加密组件.
*/
class Encryption implements IEncryption
{
/**
* 加密 key.
*/
protected string $key;
/**
* openssl 加密解密算法.
*/
protected string $cipher;
/**
* 安全 RSA 私钥.
*/
protected ?string $rsaPrivate = null;
/**
* 安全 RSA 公钥.
*/
protected ?string $rsaPublic = null;
/**
* 构造函数.
*/
public function __construct(string $key, string $cipher = 'AES-256-CBC', ?string $rsaPrivate = null, ?string $rsaPublic = null)
{
$this->validateCipher($cipher);
$this->key = $key;
$this->cipher = $cipher;
$this->rsaPrivate = $rsaPrivate;
$this->rsaPublic = $rsaPublic;
}
public function encrypt(string $value, int $expiry = 0): string
{
$iv = $this->createIv();
$value = $this->packData($value, $expiry, $iv);
return $this->encryptData($value, $iv);
}
public function decrypt(string $value): false|string
{
if (false === ($value = $this->decryptData($value))) {
return false;
}
[$data, $iv] = $value;
return $this->validateData($data, $iv);
}
/**
* 打包数据.
*/
protected function packData(string $value, int $expiry, string $iv): string
{
$data = [
$this->normalizeExpiry($expiry),
base64_encode($value),
base64_encode($iv),
$this->normalizeSign($value),
];
return implode("\t", $data);
}
/**
* 解包数据.
*/
protected function unpackData(string $value): array|false
{
$data = explode("\t", $value);
if (4 !== \count($data)) {
return false;
}
$key = ['expiry', 'value', 'iv', 'sign'];
return array_combine($key, $data);
}
/**
* 加密数据.
*
* @throws \InvalidArgumentException
*/
protected function encryptData(string $value, string $iv): string
{
$value = openssl_encrypt($value, $this->cipher, $this->key, OPENSSL_RAW_DATA, $iv);
if (false === $value) {
// 在 error_reporting(0) 场景下 openssl 加密解密算法 cipher 错误的情况下才会执行
throw new \InvalidArgumentException('Encrypt the data failed.');
}
return $this->packDataWithIv($value, $iv);
}
/**
* 解密数据.
*
* @throws \InvalidArgumentException
*/
protected function decryptData(string $value): array|false
{
if (false === ($value = base64_decode($value, true))) {
return false;
}
if (false === ($value = $this->unpackDataWithIv($value))) {
return false;
}
$data = openssl_decrypt(
$value['value'],
$this->cipher,
$this->key,
OPENSSL_RAW_DATA,
$value['iv']
);
if (false === $data) {
throw new \InvalidArgumentException('Decrypt the data failed.');
}
return [$data, base64_encode($value['iv'])];
}
/**
* 数据加入向量并打包.
*/
protected function packDataWithIv(string $value, string $iv): string
{
return base64_encode(base64_encode($value)."\t".base64_encode($iv));
}
/**
* 解包带向量的数据.
*/
protected function unpackDataWithIv(string $value): array|false
{
$data = explode("\t", $value);
if (2 !== \count($data)) {
return false;
}
$key = ['value', 'iv'];
// @phpstan-ignore-next-line
$data[0] = base64_decode($data[0], true);
// @phpstan-ignore-next-line
$data[1] = base64_decode($data[1], true);
return array_combine($key, $data);
}
/**
* 格式化过期时间.
*/
protected function normalizeExpiry(int $expiry = 0): string
{
return \sprintf('%010d', $expiry ? $expiry + time() : 0);
}
/**
* 创建初始化向量.
*
* @throws \Exception
*/
protected function createIv(): string
{
$length = openssl_cipher_iv_length($this->cipher);
if (false === $length) {
throw new \Exception('Gets the cipher iv length failed.');
}
return openssl_random_pseudo_bytes($length);
}
/**
* 获取签名.
*
* @throws \InvalidArgumentException
*/
protected function normalizeSign(string $value): string
{
if (!$this->rsaPrivate) {
return '';
}
try {
$rsaPrivate = openssl_pkey_get_private($this->rsaPrivate);
// @phpstan-ignore-next-line
if (openssl_sign($value, $sign, $rsaPrivate)) {
return base64_encode($sign);
}
// 在 error_reporting(0) 场景下签名 $rsaPrivate 错误的情况下才会执行
throw new \InvalidArgumentException('Openssl sign failed.');
} catch (\Throwable $e) {
throw new \InvalidArgumentException($e->getMessage());
}
}
/**
* 校验加密算法.
*
* @throws \InvalidArgumentException
*/
protected function validateCipher(string $cipher): void
{
if (!\in_array(strtolower($cipher), openssl_get_cipher_methods(), true)) {
throw new \InvalidArgumentException(\sprintf('Encrypt cipher `%s` was not found.', $cipher));
}
}
/**
* 校验数据正确性.
*/
protected function validateData(string $data, string $iv): false|string
{
if (false === ($data = $this->unpackData($data))) {
return false;
}
if ($data['iv'] !== $iv
|| ('0000000000' !== $data['expiry'] && time() > $data['expiry'])) {
return false;
}
$result = base64_decode($data['value'], true) ?: false;
if (false === $result) {
return false;
}
return $this->validateSign($result, $data['sign']);
}
/**
* 验证签名.
*
* @throws \InvalidArgumentException
*/
protected function validateSign(string $value, string $sign): string
{
if (!$this->rsaPublic) {
return $value;
}
try {
$rsaPublic = openssl_pkey_get_public($this->rsaPublic);
// @phpstan-ignore-next-line
if (1 === openssl_verify($value, base64_decode($sign, true), $rsaPublic)) {
return $value;
}
// 在 error_reporting(0) 场景下签名 $rsaPublic 错误的情况下才会执行
throw new \InvalidArgumentException('Openssl verify sign failed.');
} catch (\Throwable $e) {
throw new \InvalidArgumentException($e->getMessage());
}
}
}