XML digital signature with Nodejs

Asked

Viewed 1,298 times

3

Good morning

I’m using the xml-crypto and the pem to sign an XML. However after searching I did not find a way to solve this problem:

var certificado = cert.cert.toString().replace('-----BEGIN CERTIFICATE-----', '').trim().replace('-----END CERTIFICATE-----', '').trim().replace(/(\r\n\t|\n|\r\t)/gm,"");

The complete signature code is this:

pem.readPkcs12('certificado.pfx', { p12Password: "senha" }, (err, cert) => {

var certificado = cert.cert.toString().replace('-----BEGIN CERTIFICATE-----', '').trim().replace('-----END CERTIFICATE-----', '').trim().replace(/(\r\n\t|\n|\r\t)/gm,"");

var sig = new SignedXml()
sig.addReference("//*[local-name(.)='infEvento']", ['http://www.w3.org/2000/09/xmldsig#enveloped-signature', 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'])  
sig.canonicalizationAlgorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"  
sig.signingKey = fs.readFileSync('./CertPem.pem')
sig.keyInfoProvider = new myKeyInfo(certificado)
sig.computeSignature(xml, { location: {reference: "//*[local-name(.)='infEvento']", action: 'after'}})
fs.writeFileSync("signed.xml", sig.getSignedXml())});

This variable certificado is added to the tag <X509Certificate> of xml. And I couldn’t find a way to get the certificate value without using the replace in string what I get.

  • Found some solution, my friend?

  • Take this example at Node.js, I think you are looking for. https://www.example-code.com/nodejs/cert_load_from_pfx.asp

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.