kit.program/kit/test/debug_sm2_api_version.js
2026-04-04 18:06:09 +08:00

56 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('=== 调试 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);
}