admin管理员组

文章数量:1292146

` I am getting the error "Private key is required to compute signature" even though the private key is being read correctly from the file and logged. I have tried both PKCS1 and PKCS8 private key formats but the issue persists.

const privateKey = await fs.promises.readFile('./src/services/SDI-PKCS8-04126420043.pem', 'utf8'); 

// Validate private key format
if (!privateKey.includes('BEGIN PRIVATE KEY') && !privateKey.includes('BEGIN RSA PRIVATE KEY')) {
    throw new Error('Invalid Private Key format');
}

// Read certificate file
const certificate = await fs.promises.readFile('./src/services/SDI-04126420043.pem', 'utf8');

// Initialize signature
const sig = new SignedXml({
    canonicalizationAlgorithm: '',
    signatureAlgorithm: '',
});

// Assign keyInfoProvider and signingKey
sig.keyInfoProvider = new MyKeyInfo(privateKey, certificate);
sig.signingKey = Buffer.from(privateKey.trim(), 'utf8');

// Add reference to XML document
sig.addReference({
    xpath: "/*",
    transforms: [";],
    digestAlgorithm: ";
});

// Compute signature
sigputeSignature(xml);
// Get signed XML
const signedXml = sig.getSignedXml();

本文标签: nodejsHeading Private Key Error in XML Signature ComputationStack Overflow