chengkun
2025-05-26 4462855c0033970c39ac8d0da704b7dc41eabbfe
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
<?php
 
require("Autoloader.php");
 
/**
 * 第一部分:从公私钥文件路径中读取出公私钥文件内容
 */
 
$rsaPrivateKeyFilePath = 'rsa/rsa_private_key.pem';
$rsaPublicKeyFilePath = 'rsa/rsa_public_key.pem';
 
if( !file_exists($rsaPrivateKeyFilePath) || !is_readable($rsaPrivateKeyFilePath) ||
    !file_exists($rsaPublicKeyFilePath) || !is_readable($rsaPublicKeyFilePath) ){
    return false;
}
 
$rsaPrivateKeyStr = file_get_contents($rsaPrivateKeyFilePath);
$rsaPublicKeyStr = file_get_contents($rsaPublicKeyFilePath);
/**
 * 第二部分:生成签名 DEMO
 */
// $requestParamsArr = array(
//     "unitPrice"=>"1",
//     "orderId"=>"81332196309172",
//     "payTime"=>"1571997691",
//     "dealId"=>"352745305",
//     "tpOrderId"=>"157199768325055",
//     "count"=>"1",
//     "totalMoney"=>"1",
//     "hbBalanceMoney"=>"0",
//     "userId"=>"3391629990",
//     "promoMoney"=>"0",
//     "promoDetail"=>"",
//     "hbMoney"=>"0",
//     "giftCardMoney"=>"0",
//     "payMoney"=>"1",
//     "payType"=>"1087",
//     "returnData"=>"",
//     "partnerId"=>"6000001",
//     "status"=>"2"
// );
$requestParamsArr = array("unitPrice"=>"1","orderId"=>"81341494729172","payTime"=>"1572227042","dealId"=>"352745305","tpOrderId"=>"157222702731789","count"=>"1","totalMoney"=>"1","hbBalanceMoney"=>"0","userId"=>"3391629990","promoMoney"=>"0","promoDetail"=>"","hbMoney"=>"0","giftCardMoney"=>"0","payMoney"=>"1","payType"=>"1117","returnData"=>"","partnerId"=>"6000001","status"=>"2");
 
$rsaSign = NuomiRsaSign::genSignWithRsa($requestParamsArr, $rsaPrivateKeyStr);
 
$requestParamsArr['sign'] = 'm1KdXpm266xb/E0jFPXSnkHZbooLUNcuCBibcV/ImWo9qFSIwmGc5GccbRz3G7AsW1vgHHhalIwM3dAAi4YNDqUMDFgo/kSxQgw6gyzgaKVllNnAlLXQvafVt051OnJogGYZ/uMtbvkjOgUswTaWO46N6244gRNgYnyMIo9QWls=';
print_r($requestParamsArr);
 
// /**
//  * 第三部分:校验签名 DEMO 校验开发者公钥
//  */
 
// $checkSignRes = NuomiRsaSign::checkSignWithRsa($requestParamsArr, $rsaPublicKeyStr);
// var_dump($checkSignRes); # true :签名校验成功,false:签名校验失败
 
/*
 * 校验平台公钥
 */
$ptPublic = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChR8G0uWA9ah9COpBtlSY8A8M0A+gARY9YHaQXTCds9Dv+VHfsZLDx2KjxJh4eAJ6SzZajRzgrcEREKZDoTuGjm/kzTarMQ6f3YvakCJKrE4FjwNbOogBxxDJDQF3iL7Xclkv5OHuyIdPEAIYVCj3tTx4oLI9lUMq9Ocn8SBUJDwIDAQAB';
 
$ptPublicKey = convertRSAKeyStr2Pem($ptPublic);
 
$checkSignRes = NuomiRsaSign::checkSignWithRsa($requestParamsArr, $ptPublicKey);
var_dump($checkSignRes); # true :签名校验成功,false:签名校验失败
 
/**
 * @desc 密钥由字符串(不换行)转为PEM格式
 * @param $rsaKeyStr
 * @param int $keyType 0:公钥,1:私钥
 * @return string
 * @throws SF_Exception_InternalException
 */
function convertRSAKeyStr2Pem($rsaKeyStr, $keyType = 0)
{
    $rsaKeyPem = '';
    
    $beginPublicKey   = '-----BEGIN PUBLIC KEY-----';
    $endPublicKey     = '-----END PUBLIC KEY-----';
    $beginPrivateKey  = '-----BEGIN PRIVATE KEY-----';
    $endPrivateKey    = '-----END PRIVATE KEY-----';
    
    $keyPrefix = $keyType ? $beginPrivateKey : $beginPublicKey;
    $keySuffix = $keyType ? $endPrivateKey : $endPublicKey;
    
    $rsaKeyPem .= $keyPrefix. "\n";
    $rsaKeyPem .= wordwrap($rsaKeyStr, 64, "\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;
}