22
33namespace Hejunjie \EncryptedRequest ;
44
5- use Hejunjie \EncryptedRequest \Contracts \DecryptorInterface ;
65use Hejunjie \EncryptedRequest \Config \EnvConfigLoader ;
76use Hejunjie \EncryptedRequest \Drivers \AesDecryptor ;
7+ use Hejunjie \EncryptedRequest \Drivers \RsaDecryptor ;
88use Hejunjie \EncryptedRequest \Exceptions \DecryptionException ;
99use Hejunjie \EncryptedRequest \Exceptions \SignatureException ;
1010use Hejunjie \EncryptedRequest \Exceptions \TimestampException ;
1111
1212class EncryptedRequestHandler
1313{
1414 private array $ config ;
15- private DecryptorInterface $ decryptor ;
1615
1716 /**
1817 * 构造方法
1918 *
20- * @param DecryptorInterface $decryptor 解密器
2119 * @param array|string|null $config 配置数组或.env路径
2220 */
23- public function __construct (string | DecryptorInterface $ decryptorDriver = ' aes ' , array |string $ config = '' )
21+ public function __construct (array |string $ config = '' )
2422 {
2523 if (is_array ($ config )) {
2624 $ loader = new EnvConfigLoader ($ config );
@@ -30,23 +28,9 @@ public function __construct(string|DecryptorInterface $decryptorDriver = 'aes',
3028 $ loader = new EnvConfigLoader ();
3129 }
3230 $ this ->config = [
33- 'key ' => $ loader ->get ('APP_KEY ' ),
34- 'default_timestamp_diff ' => $ loader ->get ('DEFAULT_TIMESTAMP_DIFF ' , 60 )
31+ 'default_timestamp_diff ' => $ loader ->get ('DEFAULT_TIMESTAMP_DIFF ' , 60 ),
32+ 'rsa_private_key ' => $ loader ->get ('RSA_PRIVATE_KEY ' , '' )
3533 ];
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- }
5034 }
5135
5236 /**
@@ -61,24 +45,30 @@ public function __construct(string|DecryptorInterface $decryptorDriver = 'aes',
6145 * @throws TimestampException 请求时间错误
6246 * @throws DecryptionException 数据解密错误
6347 */
64- public function handle (string $ en_data , int $ timestamp , string $ sign ): array
48+ public function handle (string $ en_data , string $ enc_payload , int $ timestamp , string $ sign ): array
6549 {
66- // 1. 签名验证
6750 if (!isset ($ timestamp , $ sign , $ en_data )) {
6851 throw new SignatureException ("Missing required parameters " );
6952 }
70- $ expectedSign = md5 ($ this ->config ['key ' ] . $ timestamp );
71- if ($ expectedSign !== $ sign ) {
72- throw new SignatureException ("Invalid signature " );
73- }
74- // 2. 时间戳验证
53+ // 1. 时间戳验证
7554 $ timestampDiff = $ this ->config ['default_timestamp_diff ' ];
7655 $ now = time ();
7756 if (abs ($ now - $ timestamp ) > $ timestampDiff ) {
7857 throw new TimestampException ("Timestamp difference too large " );
7958 }
80- // 3. 解密数据
81- $ decrypted = $ this ->decryptor ->decrypt ($ en_data );
59+ // 2. 获取签名数据
60+ $ rsa = new RsaDecryptor ($ this ->config ['rsa_private_key ' ]);
61+ $ enc_payload = $ rsa ->decrypt ($ enc_payload );
62+ // 3. 签名验证
63+ $ expectedSign = md5 (md5 ($ enc_payload ) . $ timestamp );
64+ if ($ expectedSign !== $ sign ) {
65+ throw new SignatureException ("Invalid signature " );
66+ }
67+ // 4. 解密数据
68+ $ aesKeyBase64 = substr ($ enc_payload , 0 , 24 );
69+ $ aesIvBase64 = substr ($ enc_payload , -24 );
70+ $ aes = new AesDecryptor (base64_decode ($ aesKeyBase64 ), base64_decode ($ aesIvBase64 ));
71+ $ decrypted = $ aes ->decrypt ($ en_data );
8272 return $ decrypted ;
8373 }
8474}
0 commit comments