44
55use Hejunjie \EncryptedRequest \Contracts \DecryptorInterface ;
66use Hejunjie \EncryptedRequest \Config \EnvConfigLoader ;
7+ use Hejunjie \EncryptedRequest \Drivers \AesDecryptor ;
78use Hejunjie \EncryptedRequest \Exceptions \DecryptionException ;
89use Hejunjie \EncryptedRequest \Exceptions \SignatureException ;
910use 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