我正在尝试实施支付系统,我获得了一个用PHP编写的ssl证书验证的示例代码。代码如下:
echo "<strong>1. Verify Certifcate</strong><blockquote>";
#Get DigestValue
echo "<strong>DigestValue:</strong><br>",$sigDigest=$xmlDoc-
>documentElement->getElementsByTagName("DigestValue")->item(0)->nodeValue,"
<br>";
$signedInfo=$xmlDoc->getElementsByTagName("SignedInfo")->item(0)->C14N(true,
true);
#Get SignatureValue
$signature = base64_decode($xmlDoc->documentElement-
>getElementsByTagName("SignatureValue")->item(0)->nodeValue);
#Check Certificate
$ok = openssl_verify($signedInfo, $signature, $pubKey, OPENSSL_ALGO_SHA1);
echo "<p><strong>Status:</strong> ", $ok == 1 ? "Valid" : "Invalid", "
Cetificate.</p></blockquote><hr>";
我面临的问题是我正在尝试在Node.js上做同样的事情但是当我尝试验证它时,它每次都返回false。我知道这里有一个类似的问题,我尝试了他的解决方案,但这个问题并没有奏效。
基本上,我正在努力做同样的事情。
$ok = openssl_verify($signedInfo, $signature, $pubKey, OPENSSL_ALGO_SHA1);
这是代码。验证方法在checkSignature函数中。
exports.verifyTest = function (req, res) {
var keyPath = 'certs/result.xml';
var xml = fs.readFileSync(keyPath);
var parseString = require('xml2js').parseString;
parseString(xml,
{trim : true },
function (err, result) {
if(result.Message && result.Message.OrderStatus == 'APPROVED') {
var orderId = result.Message.OrderID,
signature = result['Message']['Signature'][0]['SignatureValue'],
signedInfo = result['Message']['Signature'][0]['SignedInfo'][0]
['Reference'][0]['DigestValue'];
var valid = checkSignature(signedInfo+"", signature+"");
return res.status(400).send({
message: "Гүйлгээ амжилттай " + valid
});
} else {
return res.status(400).send({
message: "Unsuccessful"
});
}
});
};
function checkSignature(data, sig) {
var pub = fs.readFileSync('certs/public.cert').toString()
var verify = crypto.createVerify('RSA-SHA1');
verify.update(data);
var result = verify.verify(pub,sig,'base64');
return result;
}
任何帮助都将不胜感激。