1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace app\common\payment;
- use Payment\Common\PayException;
- class AppUtil
- {
- /**
- * 将参数数组签名
- */
- //RSA签名
- public static function Sign(array $array)
- {
- ksort($array);
- $bufSignSrc = AppUtil::ToUrlParams($array);
- $private_key = AppConfig::PRIVATEKEY;
- $private_key = chunk_split($private_key, 64, "\n");
- $key = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($private_key) . "-----END RSA PRIVATE KEY-----";
- // echo $key;
- if (openssl_sign($bufSignSrc, $signature, $key)) {
- $sign = base64_encode($signature);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
- } else {
- throw new PayException('签名错误');
- }
- return $sign;
- }
- public static function ToUrlParams(array $array)
- {
- $buff = "";
- foreach ($array as $k => $v) {
- if ($v != "" && !is_array($v)) {
- $buff .= $k . "=" . $v . "&";
- }
- }
- $buff = trim($buff, "&");
- return $buff;
- }
- /**
- * 校验签名
- * @param array 参数
- * @param unknown_type appkey
- */
- public static function ValidSign(array $array)
- {
- $sign = $array['sign'];
- unset($array['sign']);
- ksort($array);
- $bufSignSrc = AppUtil::ToUrlParams($array);
- $public_key = AppConfig::PUBLICKEY;
- $public_key = chunk_split($public_key, 64, "\n");
- $key = "-----BEGIN PUBLIC KEY-----\n$public_key-----END PUBLIC KEY-----\n";
- $result = openssl_verify($bufSignSrc, base64_decode($sign), $key);
- return $result;
- }
- }
- ?>
|