secureUtil.php
5.32 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
use WebPlugin\UdpLog;
include_once 'PublicEncrypte.php';
/**
* 签名
*
* @param String $params_str
*/
function sign(&$params)
{
if (isset($params['transTempUrl'])) {
unset($params['transTempUrl']);
}
// 转换成key=val&串
$params_str = coverParamsToString($params);
$params_sha1x16 = sha1($params_str, FALSE);
// 签名证书路径
$cert_path = dirname(__FILE__) . '/../' . SDK_SIGN_CERT_PATH;
$private_key = getPrivateKey($cert_path);
// 签名
$sign_falg = openssl_sign($params_sha1x16, $signature, $private_key, OPENSSL_ALGO_SHA1);
if ($sign_falg) {
$signature_base64 = base64_encode($signature);
$params ['signature'] = $signature_base64;
}
UdpLog::info("【Unionpayweb-签名】,function:sign,参数,", $params);
}
/**
* 验签
*
* @param String $params_str
* @param String $signature_str
*/
function verify($params)
{
// 公钥
$public_key = getPulbicKeyByCertId($params['certId']);
// 签名串
$signature_str = $params['signature'];
unset($params['signature']);
$params_str = coverParamsToString($params);
$signature = base64_decode($signature_str);
$params_sha1x16 = sha1($params_str, FALSE);
$isSuccess = openssl_verify($params_sha1x16, $signature, $public_key, OPENSSL_ALGO_SHA1);
UdpLog::info("【Unionpayweb-验签,function:verify,参数,", $params);
return $isSuccess;
}
/**
* 根据证书ID 加载 证书
*
* @param unknown_type $certId
* @return string NULL
*/
function getPulbicKeyByCertId($certId)
{
// 证书目录
$cert_dir = dirname(__FILE__) . '/../' . SDK_VERIFY_CERT_DIR;
$handle = opendir($cert_dir);
if ($handle) {
while ($file = readdir($handle)) {
clearstatcache();
$filePath = $cert_dir . '/' . $file;
if (is_file($filePath)) {
if (pathinfo($file, PATHINFO_EXTENSION) == 'cer') {
if (getCertIdByCerPath($filePath) == $certId) {
closedir($handle);
return getPublicKey($filePath);
}
}
}
}
}
closedir($handle);
return null;
}
/**
* 取证书ID(.pfx)
*
* @return unknown
*/
function getCertId($cert_path)
{
$pkcs12certdata = file_get_contents($cert_path);
openssl_pkcs12_read($pkcs12certdata, $certs, SDK_SIGN_CERT_PWD);
$x509data = $certs ['cert'];
openssl_x509_read($x509data);
$certdata = openssl_x509_parse($x509data);
$cert_id = $certdata ['serialNumber'];
return $cert_id;
}
/**
* 取证书ID(.cer)
*
* @param unknown_type $cert_path
*/
function getCertIdByCerPath($cert_path)
{
$x509data = file_get_contents($cert_path);
openssl_x509_read($x509data);
$certdata = openssl_x509_parse($x509data);
$cert_id = $certdata ['serialNumber'];
return $cert_id;
}
/**
* 签名证书ID
*
* @return unknown
*/
function getSignCertId()
{
// 签名证书路径
return getCertId(dirname(__FILE__) . '/../' . SDK_SIGN_CERT_PATH);
}
function getEncryptCertId()
{
// 签名证书路径
return getCertIdByCerPath(dirname(__FILE__) . '/../' . SDK_ENCRYPT_CERT_PATH);
}
/**
* 取证书公钥 -验签
*
* @return string
*/
function getPublicKey($cert_path)
{
return file_get_contents($cert_path);
}
/**
* 返回(签名)证书私钥 -
*
* @return unknown
*/
function getPrivateKey($cert_path)
{
$pkcs12 = file_get_contents($cert_path);
openssl_pkcs12_read($pkcs12, $certs, SDK_SIGN_CERT_PWD);
return $certs ['pkey'];
}
/**
* 加密 卡号
*
* @param String $pan
* 卡号
* @return String
*/
function encryptPan($pan)
{
$cert_path = MPI_ENCRYPT_CERT_PATH;
$public_key = getPublicKey($cert_path);
openssl_public_encrypt($pan, $cryptPan, $public_key);
return base64_encode($cryptPan);
}
/**
* pin 加密
*
* @param unknown_type $pan
* @param unknown_type $pwd
* @return Ambigous <number, string>
*/
function encryptPin($pan, $pwd)
{
$cert_path = dirname(__FILE__) . '/../' . SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey($cert_path);
return EncryptedPin($pwd, $pan, $public_key);
}
/**
* cvn2 加密
*
* @param unknown_type $cvn2
* @return unknown
*/
function encryptCvn2($cvn2)
{
$cert_path = dirname(__FILE__) . '/../' . SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey($cert_path);
openssl_public_encrypt($cvn2, $crypted, $public_key);
return base64_encode($crypted);
}
/**
* 加密 有效期
*
* @param unknown_type $certDate
* @return unknown
*/
function encryptDate($certDate)
{
$cert_path = dirname(__FILE__) . '/../' . SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey($cert_path);
openssl_public_encrypt($certDate, $crypted, $public_key);
return base64_encode($crypted);
}
/**
* 加密 数据
*
* @param unknown_type $certDatatype
* @return unknown
*/
function encryptDateType($certDataType)
{
$cert_path = dirname(__FILE__) . '/../' . SDK_ENCRYPT_CERT_PATH;
$public_key = getPublicKey($cert_path);
openssl_public_encrypt($certDataType, $crypted, $public_key);
return base64_encode($crypted);
}
?>