46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
const sm2 = require('sm-crypto').sm2;
|
||
const sm3 = require('sm-crypto').sm3;
|
||
|
||
console.log('=== 调试 SM2 公钥格式 ===');
|
||
|
||
// 测试数据
|
||
const testData = 'QDJNHHVB_FWQ';
|
||
const hash = sm3(testData);
|
||
console.log('测试数据:', testData);
|
||
console.log('SM3 哈希:', hash);
|
||
|
||
// 用户提供的签名(ASN.1 DER 格式)
|
||
const userSignature = '3044022002d3816303ee2a352477f3227070cc34297bd6e89dc86e2c54840fffc92c3b67022066114b6bfacb00408cc51b8c18d02023f50671844372094d9460bd5f85acac50';
|
||
|
||
// 解析签名
|
||
const rStart = 8;
|
||
const r = userSignature.substring(rStart, rStart + 64);
|
||
const sStart = rStart + 64 + 4;
|
||
const s = userSignature.substring(sStart, sStart + 64);
|
||
const rawSignature = r + s;
|
||
console.log('\n原始签名格式:', rawSignature);
|
||
|
||
// 用户公钥
|
||
const userPublicKey = '04bb7754e559f6d158d52fdc4608244f07f12901363a1d6db8fb3219bf8e827efd11d8f18cf9926cfdd61aaf0eea2429e921b7b94f43ca9b2c89213355ad59e731';
|
||
console.log('\n完整公钥:', userPublicKey);
|
||
|
||
// 去掉 04 前缀
|
||
const pubKeyWithoutPrefix = userPublicKey.substring(2);
|
||
console.log('去掉前缀的公钥:', pubKeyWithoutPrefix);
|
||
|
||
// 尝试验证
|
||
console.log('\n验证结果:');
|
||
try {
|
||
const result1 = sm2.doVerifySignature(hash, rawSignature, userPublicKey);
|
||
console.log(' 使用完整公钥:', result1);
|
||
} catch (e) {
|
||
console.error(' 完整公钥错误:', e.message);
|
||
}
|
||
|
||
try {
|
||
const result2 = sm2.doVerifySignature(hash, rawSignature, pubKeyWithoutPrefix);
|
||
console.log(' 使用去掉前缀的公钥:', result2);
|
||
} catch (e) {
|
||
console.error(' 去掉前缀错误:', e.message);
|
||
}
|