验证失败

This commit is contained in:
cheney
2026-04-04 18:06:09 +08:00
parent 959f058def
commit 03169b5165
6 changed files with 285 additions and 34 deletions
+55
View File
@@ -0,0 +1,55 @@
const sm2 = require('sm-crypto').sm2;
const sm3 = require('sm-crypto').sm3;
console.log('=== 调试 sm-crypto 0.3.13 API ===');
// 测试基本功能
const testData = 'test';
const hash = sm3(testData);
console.log('测试数据:', testData);
console.log('SM3 哈希:', hash);
// 生成密钥对
const keypair = sm2.generateKeyPairHex();
console.log('\n生成的密钥对:');
console.log(' 私钥:', keypair.privateKey);
console.log(' 公钥:', keypair.publicKey);
// 签名
const sig = sm2.doSignature(testData, keypair.privateKey);
console.log('\n签名:', sig);
console.log('签名长度:', sig.length);
// 验证签名
const verifyResult = sm2.doVerifySignature(testData, sig, keypair.publicKey);
console.log('验证结果:', verifyResult);
// 测试用户提供的数据
console.log('\n=== 测试用户数据 ===');
const data = 'QDJNHHVB_FWQ';
const signature = '02D3816303EE2A352477F3227070CC34297BD6E89DC86E2C54840FFFC92C3B6766114B6BFA CB00408CC51B8C18D02023F50671844372094D9460BD5F85ACAC50'.replace(/\s/g, '');
const publicKey = '04bb7754e559f6d158d52fdc4608244f07f12901363a1d6db8fb3219bf8e827efd11d8f18cf9926cfdd61aaf0eea2429e921b7b94f43ca9b2c89213355ad59e731';
console.log('数据:', data);
console.log('签名:', signature);
console.log('公钥:', publicKey);
// 尝试验证
console.log('\n尝试验证:');
try {
// 注意:sm-crypto 0.3.13 的 API 可能不同
// 尝试不带选项
const result1 = sm2.doVerifySignature(data, signature, publicKey);
console.log(' 验证结果 (无选项):', result1);
} catch (e) {
console.error(' 错误 (无选项):', e.message);
}
try {
// 尝试带哈希选项
const result2 = sm2.doVerifySignature(hash, signature, publicKey, true); // 第二个参数是 hash 选项
console.log(' 验证结果 (hash=true):', result2);
} catch (e) {
console.error(' 错误 (hash=true):', e.message);
}