Closed Messilimeng closed 3 months ago
@Messilimeng 谢谢作者的辛勤劳动,代码功能正常,对我的帮助很大。 我在调试的时候发现在mac平台和linux平台下计算结果不一致,mac下是正确的,linux下不对,跟踪后我发现问题出现在CalculateMd5这个函数中,我将bigInt(SerialNumber,16).value.toString().replace('n','')改为bigInt(SerialNumber,16).toString().replace('n','')这个后就可以了。
@CMangoDH (๑•̀ㅂ•́)و✧ 棒
var fs = require('fs') var x509 = require('@fidm/x509') var crypto = require('crypto'); var bigInt = require("big-integer");
var RootCertContent = fs.readFileSync('./src/alipayRootCert.crt', 'utf8') var alipayCertPublicKey_RSA2 = fs.readFileSync('./src/alipayCertPublicKey_RSA2.crt', 'utf8')
var alipayPublicKeyCert = fs.readFileSync('./src/appCertPublicKey_2016xxxxxx46.crt', 'utf8')
GetRootCertSN(RootCertContent) GetAppCertSN(alipayCertPublicKey_RSA2) GetalipayPublicKeyCertSN(alipayPublicKeyCert)
function GetRootCertSN(RootCertContent, signType = 'RSA2') { var rootCertSN = ""; var RootCertlist = RootCertContent.split('-----END CERTIFICATE-----') for (const iterator of RootCertlist) { if (iterator.trim() != "") { var line = iterator.trim() + '\n' + '-----END CERTIFICATE-----' const RootCertjson = x509.Certificate.fromPEM(line) if (signType.startsWith('RSA') && RootCertjson.signatureOID.startsWith('1.2.840.113549.1.1')) { var certSN = GetCertSN(RootCertjson) if (rootCertSN == '') { rootCertSN = certSN } else { rootCertSN = rootCertSN + "_" + certSN } }
}
function GetAppCertSN(alipayCertPublicKey_RSA2) { const Certjson = x509.Certificate.fromPEM(alipayCertPublicKey_RSA2) const certSN = GetCertSN(Certjson) console.warn('GetAppCertSN:'+certSN) }
function GetalipayPublicKeyCertSN(alipayPublicKeyCert) { const Certjson = x509.Certificate.fromPEM(alipayPublicKeyCert) //console.log(Certjson) var certSN = GetCertSN(Certjson) console.warn('GetalipayPublicKeyCertSN:'+certSN)
}
function GetCertSN(Certjson) { var attributes = Certjson.issuer.attributes var issuerD = '' var SerialNumber = Certjson.serialNumber for (const iterator of attributes) { issuerD += (iterator.shortName + '=' + iterator.value + ',') } return CalculateMd5(issuerD, SerialNumber) } function CalculateMd5(issuerD, SerialNumber) { var isduerDArray = issuerD.split(',').reverse() isduerDArray.shift() var input = isduerDArray.join(',') + bigInt(SerialNumber, 16).value.toString().replace('n', '') var md5 = crypto.createHash('md5'); var result = md5.update(input).digest('hex') return result }