rsa p7 info 通过
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
const forge = require('node-forge');
|
||||
|
||||
console.log('=== 生成测试用的 PKCS7 签名 ===');
|
||||
|
||||
try {
|
||||
// 1. 生成 RSA 密钥对
|
||||
console.log('生成 RSA 密钥对...');
|
||||
const keys = forge.pki.rsa.generateKeyPair(1024);
|
||||
const privateKey = keys.privateKey;
|
||||
const publicKey = keys.publicKey;
|
||||
|
||||
// 2. 创建自签名证书
|
||||
console.log('创建自签名证书...');
|
||||
const cert = forge.pki.createCertificate();
|
||||
cert.publicKey = publicKey;
|
||||
cert.serialNumber = '01';
|
||||
cert.validity.notBefore = new Date();
|
||||
cert.validity.notAfter = new Date();
|
||||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
||||
|
||||
const attrs = [
|
||||
{ name: 'commonName', value: 'test.example.com' },
|
||||
{ name: 'organizationName', value: 'Test Organization' }
|
||||
];
|
||||
cert.setSubject(attrs);
|
||||
cert.setIssuer(attrs);
|
||||
cert.sign(privateKey, forge.md.sha256.create());
|
||||
|
||||
// 3. 创建 PKCS7 签名
|
||||
console.log('创建 PKCS7 签名...');
|
||||
const p7 = forge.pkcs7.createSignedData();
|
||||
p7.content = forge.util.createBuffer('This is the test content for PKCS7 signature.');
|
||||
p7.addCertificate(cert);
|
||||
p7.addSigner({
|
||||
key: privateKey,
|
||||
certificate: cert,
|
||||
digestAlgorithm: forge.pki.oids.sha256
|
||||
});
|
||||
p7.sign();
|
||||
|
||||
// 4. 转换为 PEM 格式
|
||||
const pem = forge.pkcs7.messageToPem(p7);
|
||||
|
||||
console.log('\n=== 生成的 PKCS7 签名 PEM 格式 ===');
|
||||
console.log(pem);
|
||||
|
||||
console.log('\n=== 提取 base64 部分 ===');
|
||||
const base64 = pem.replace(/-----BEGIN PKCS7-----\n?/, '')
|
||||
.replace(/-----END PKCS7-----\n?/, '')
|
||||
.replace(/\n/g, '');
|
||||
console.log(base64);
|
||||
|
||||
console.log('\n=== 完成 ===');
|
||||
} catch (error) {
|
||||
console.error('生成失败:', error);
|
||||
console.error(error.stack);
|
||||
}
|
||||
Reference in New Issue
Block a user