kit.program/kit/test/generate_sm2_p7.js
2026-04-04 17:29:00 +08:00

32 lines
1.1 KiB
JavaScript
Raw 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;
const forge = require('node-forge');
console.log('=== 生成 SM2 测试数据 ===');
// 生成 SM2 密钥对
const keypair = sm2.generateKeyPairHex();
console.log('私钥:', keypair.privateKey);
console.log('公钥:', keypair.publicKey);
// 测试数据
const originalData = 'This is test data for SM2 signature.';
console.log('\n原始数据:', originalData);
// 计算 SM3 哈希
const hash = sm3(originalData);
console.log('SM3 哈希:', hash);
// SM2 签名
const sig = sm2.doSignature(hash, keypair.privateKey);
console.log('SM2 签名:', sig);
// 验证签名
const verifyResult = sm2.doVerify(hash, sig, keypair.publicKey);
console.log('签名验证结果:', verifyResult);
console.log('\n=== 注意 ===');
console.log('由于 node-forge 不支持 SM2 算法,无法生成标准的 SM2 PKCS7 签名。');
console.log('SM2 PKCS7 签名需要特殊的 OID 和格式,目前仅支持 RSA PKCS7。');
console.log('建议测试2可以使用 RSA 的 PKCS7 数据,或者跳过 SM2 测试。');