| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace addons\JoinPay\common\logic\util;
- use ReflectionClass;
- use ReflectionProperty;
- use addons\JoinPay\common\logic\exceptions\SDKException;
- /**
- * 签名、验签工具类
- * Class SignUtil
- * @package utils
- */
- class SignUtil {
- const MS5_BOUND_SYMBOL = "&key=";
- /**
- * 签名
- * @param string $signData 需要签名的数据
- * @param string $signType 签名类型
- * @param string $priKey 用以签名的私钥
- * @return string
- * @throws SDKException
- */
- public static function sign(string $signData, string $signType, string $priKey){
- if("2" === $signType){
- return RSAUtil::sign($signData, $priKey);
- }else if("1" === $signType){
- return MD5Util::getMd5Str($signData . self::MS5_BOUND_SYMBOL . $priKey);
- }else{
- throw new SDKException(SDKException::BIZ_ERROR, "未支持的签名类型:" . $signType);
- }
- }
- /**
- * 验签
- * @param string $signData 需要验签的数据
- * @param string $signParam 需要被校验签名的源数据
- * @param string $signType 签名类型
- * @param string $pubKey 用以验签的公钥
- * @return bool
- * @throws SDKException
- */
- public static function verify(string $signData, string $signParam, string $signType, string $pubKey){
- if("2" === $signType){
- return RSAUtil::verify($signData, $signParam, $pubKey);
- }else if("1" === $signType){
- $signData = MD5Util::getMd5Str($signData . self::MS5_BOUND_SYMBOL . $pubKey);
- return $signData === $signParam;
- }else{
- throw new SDKException(SDKException::BIZ_ERROR, "未支持的签名类型:" . $signType);
- }
- }
- /**
- * 取得 待签名/待验签 的字符串
- * @param object $param
- * @return string
- * @throws \ReflectionException
- */
- public static function getSortedString(object $param){
- $reflect = new ReflectionClass($param);
- $props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED);
- //通过反射取得所有属性和属性的值
- $arr = [];
- foreach ($props as $prop) {
- $prop->setAccessible(true);
- $key = $prop->getName();
- $value = $prop->getValue($param);
- $arr[$key] = $value;
- }
- //按key的字典序升序排序,并保留key值
- ksort($arr);
- //拼接字符串
- $str = '';
- $i = 0;
- foreach($arr as $key => $value) {
- //不参与签名、验签
- if($key == "sign" || $key == "sec_key"){
- continue;
- }
- if($key == 'resp_code' && !$value){
- continue;
- }
- if($value === null){
- $value = '';
- }
- if($i !== 0){
- $str .= '&';
- }
- $str .= $key . '=' . $value;
- $i ++;
- }
- return $str;
- }
- }
|