kit.program/kit/test/debug_sm2_signature_format2.js
2026-04-04 17:54:13 +08:00

51 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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';
console.log('\n用户提供的签名:', userSignature);
// 正确解析 ASN.1 DER 格式
console.log('\n正确解析 ASN.1 DER 格式:');
try {
// 30 44 02 20 [r] 02 20 [s]
// 30 = 序列
// 44 = 长度 68 字节
// 02 = 整数
// 20 = 长度 32 字节
// 提取 r 值
const rStart = 8; // 30440220
const r = userSignature.substring(rStart, rStart + 64); // 32字节 = 64 hex 字符
console.log(' r 值:', r);
// 提取 s 值
const sStart = rStart + 64 + 4; // 0220
const s = userSignature.substring(sStart, sStart + 64); // 32字节 = 64 hex 字符
console.log(' s 值:', s);
// 组合成原始格式 (r + s)
const rawSignature = r + s;
console.log(' 转换后的原始格式:', rawSignature);
console.log(' 原始格式长度:', rawSignature.length);
// 用户公钥
const userPublicKey = '04bb7754e559f6d158d52fdc4608244f07f12901363a1d6db8fb3219bf8e827efd11d8f18cf9926cfdd61aaf0eea2429e921b7b94f43ca9b2c89213355ad59e731';
console.log('\n用户公钥:', userPublicKey);
// 尝试验证
const result = sm2.doVerifySignature(hash, rawSignature, userPublicKey);
console.log('\n验证结果:', result);
} catch (e) {
console.error('解析错误:', e.message);
}