chengkun
2025-05-12 c49b17b9588306c14ad4b30e6a2c4b8644f3233b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
 
 
function baiduOrder($data)
{
    
    require (DATA_PATH."api/baidu/baidu_data.php");
    
    require("Autoloader.php");
    
    $requestParamsArr = array(
        'dealId'       =>  $baiduData['sy_dealId'],
        'appKey'       =>  $baiduData['sy_appKey'],
        'totalAmount'  =>  $data['totalAmount']*100,
        'tpOrderId'    =>  $data['tpOrderId'],
    );
    
    $requestParamsArr['rsaSign']  =  rsaSign($requestParamsArr, $baiduData['sy_privateKey']);
    
    $bizInfo  =  array(
        'tpData'=> $requestParamsArr
    );
    return $bizInfo;
}
/**
 * 签名
 */
function rsaSign($requestParamsArr, $privateKey = '')
{
    if (empty($privateKey)){
        echo 'no rsa_private_key';die;
    }
    
    $rsaStr  =  convertRSAKeyStr2Pem($privateKey,1);
    
    $rsaSign = NuomiRsaSign::genSignWithRsa($requestParamsArr, $rsaStr);
    
    $requestParamsArr['sign']  =  $rsaSign;
    
    return $rsaSign;
}
/**
 * 签名
 */
function checkSign($requestParamsArr,$keyType = 0)
{
    if ($keyType == 0){
        // 获取平台公钥
        require_once(dirname(dirname(dirname(__FILE__)))."/data/api/baidu/baidu_data.php");
        
        $ptPublic = $baiduData['sy_publicKey'];
        
        if (empty($ptPublic)){
            echo 'no ptPublic';die;
        }
        $rsaStr = convertRSAKeyStr2Pem($ptPublic);
        
    }else{
        // 获取用户私钥
        require (DATA_PATH."api/baidu/baidu_data.php");
        
        $privateKey  =  $baiduData['sy_privateKey'];
        
        if (empty($privateKey)){
            echo 'no rsa_private_key';die;
        }
        
        $rsaStr  =  convertRSAKeyStr2Pem($privateKey,1);
    }
    
    $checkSignRes = NuomiRsaSign::checkSignWithRsa($requestParamsArr, $rsaStr);
    
    return $checkSignRes;
}
/**
 * @desc 密钥由字符串(不换行)转为PEM格式(用户网站保存的公钥转为PEM格式)
 * @param $rsaKeyStr
 * @param int $keyType 0:公钥,1:私钥
 * @return string
 * @throws SF_Exception_InternalException
 */
function convertRSAKeyStr2Pem($rsaKeyStr, $keyType = 0)
{
    $pemWidth = 64;
    $rsaKeyPem = '';
    
    $begin = '-----BEGIN ';
    $end = '-----END ';
    $key = ' KEY-----';
    $type = $keyType ? 'RSA PRIVATE' : 'PUBLIC';
    
    $keyPrefix = $begin . $type . $key;
    $keySuffix = $end . $type . $key;
    
    $rsaKeyPem .= $keyPrefix . "\n";
    $rsaKeyPem .= wordwrap($rsaKeyStr, $pemWidth, "\n", true) . "\n";
    $rsaKeyPem .= $keySuffix;
    
    if(!function_exists('openssl_pkey_get_public') || !function_exists('openssl_pkey_get_private')){
        return false;
    }
    
    if($keyType == 0 && false == openssl_pkey_get_public($rsaKeyPem)){
        return false;
    }
    
    if($keyType == 1 && false == openssl_pkey_get_private($rsaKeyPem)){
        return false;
    }
    
    return $rsaKeyPem;
}
?>