Skip to content

Commit 190637b

Browse files
author
苏青安
committed
feat(core): 更新 EncryptedRequestHandler 构造方法以支持字符串解密器驱动
1 parent 3eab75b commit 190637b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/EncryptedRequestHandler.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Hejunjie\EncryptedRequest\Contracts\DecryptorInterface;
66
use Hejunjie\EncryptedRequest\Config\EnvConfigLoader;
7+
use Hejunjie\EncryptedRequest\Drivers\AesDecryptor;
78
use Hejunjie\EncryptedRequest\Exceptions\DecryptionException;
89
use Hejunjie\EncryptedRequest\Exceptions\SignatureException;
910
use Hejunjie\EncryptedRequest\Exceptions\TimestampException;
@@ -19,7 +20,7 @@ class EncryptedRequestHandler
1920
* @param DecryptorInterface $decryptor 解密器
2021
* @param array|string|null $config 配置数组或.env路径
2122
*/
22-
public function __construct(DecryptorInterface $decryptor, array|string $config = '')
23+
public function __construct(string|DecryptorInterface $decryptorDriver = 'aes', array|string $config = '')
2324
{
2425
if (is_array($config)) {
2526
$loader = new EnvConfigLoader($config);
@@ -30,12 +31,22 @@ public function __construct(DecryptorInterface $decryptor, array|string $config
3031
}
3132
$this->config = [
3233
'key' => $loader->get('APP_KEY'),
33-
'aes_key' => $loader->get('AES_KEY'),
34-
'aes_iv' => $loader->get('AES_IV'),
3534
'default_timestamp_diff' => $loader->get('DEFAULT_TIMESTAMP_DIFF', 60)
3635
];
37-
38-
$this->decryptor = $decryptor;
36+
// 判断是字符串还是实例
37+
if ($decryptorDriver instanceof DecryptorInterface) {
38+
$this->decryptor = $decryptorDriver;
39+
} elseif (is_string($decryptorDriver)) {
40+
switch (strtolower($decryptorDriver)) {
41+
case 'aes':
42+
$this->decryptor = new AesDecryptor($loader->get('AES_KEY'), $loader->get('AES_IV'));
43+
break;
44+
default:
45+
throw new DecryptionException("Unsupported decryptor driver: {$decryptorDriver}");
46+
}
47+
} else {
48+
throw new DecryptionException("Invalid decryptor provided");
49+
}
3950
}
4051

4152
/**

0 commit comments

Comments
 (0)