From ddf8b1d49bbe7e7728e3e8b565b8c344bd811d88 Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Tue, 2 Apr 2024 14:52:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/YopSign/YopSignUtils.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/YopSign/YopSignUtils.php b/src/YopSign/YopSignUtils.php index e15f91b..3ced288 100644 --- a/src/YopSign/YopSignUtils.php +++ b/src/YopSign/YopSignUtils.php @@ -7,6 +7,7 @@ class YopSignUtils /** * 签名生成算法 + * * @param array $params API调用的请求参数集合的关联数组,不包含sign参数 * @param array $ignoreParamNames 忽略的参数数组 * @param String $secret 密钥 @@ -19,8 +20,8 @@ class YopSignUtils $requestparams = $params; ksort($requestparams); foreach ($requestparams as $k => $v) { - if (!in_array($k, $ignoreParamNames)) { - if (!($v === null)) { + if (! in_array($k, $ignoreParamNames)) { + if (! ($v === null)) { $str .= "$k$v"; } } @@ -35,6 +36,7 @@ class YopSignUtils /** * 签名验证算法 + * * @param array $result API调用的请求参数集合的关联数组,不包含sign参数 * @param String $secret 密钥 * @param String $algName 加密算法 @@ -50,14 +52,13 @@ class YopSignUtils } else { return false; } - } public static function decrypt($source, $private_Key, $public_Key) { $private_key = "-----BEGIN RSA PRIVATE KEY-----\n". - wordwrap($private_Key, 64, "\n", true). - "\n-----END RSA PRIVATE KEY-----"; + wordwrap($private_Key, 64, "\n", true). + "\n-----END RSA PRIVATE KEY-----"; extension_loaded('openssl') or die('php需要openssl扩展支持'); /* 提取私钥 */ @@ -73,6 +74,7 @@ class YopSignUtils $encryptedDataToBase64 = $args[1]; $symmetricEncryptAlg = $args[2]; $digestAlg = $args[3]; + //用私钥对随机密钥进行解密 openssl_private_decrypt(Base64Url::decode($encryptedRandomKeyToBase64), $randomKey, $privateKey); openssl_free_key($privateKey); @@ -82,11 +84,12 @@ class YopSignUtils $signToBase64 = substr(strrchr($encryptedData, '$'), 1); $sourceData = substr($encryptedData, 0, strlen($encryptedData) - strlen($signToBase64) - 1); $public_key = "-----BEGIN PUBLIC KEY-----\n". - wordwrap($public_Key, 64, "\n", true). - "\n-----END PUBLIC KEY-----"; + wordwrap($public_Key, 64, "\n", true). + "\n-----END PUBLIC KEY-----"; $publicKey = openssl_pkey_get_public($public_key); $res = openssl_verify($sourceData, Base64Url::decode($signToBase64), $publicKey, $digestAlg); //验证 openssl_free_key($publicKey); + if ($res == 1) { return $sourceData; } else { @@ -97,8 +100,8 @@ class YopSignUtils public static function signRsa($source, $private_Key) { $private_key = "-----BEGIN RSA PRIVATE KEY-----\n". - wordwrap($private_Key, 64, "\n", true). - "\n-----END RSA PRIVATE KEY-----"; + wordwrap($private_Key, 64, "\n", true). + "\n-----END RSA PRIVATE KEY-----"; extension_loaded('openssl') or die('php需要openssl扩展支持'); /* 提取私钥 */ $privateKey = openssl_get_privatekey($private_key); @@ -121,7 +124,6 @@ class YopSignUtils $prikeyid = preg_replace("/(\r\n|\n|\r|\t)/i", '', $prikeyid); return $prikeyid; - } } \ No newline at end of file