XML digital signature with NodeJs

2

Good morning

I'm using xml-crypto and pem to sign an XML. But after searching I could not find a way to solve this gambiarra:

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

The complete subscription 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 in the <X509Certificate> tag of the xml. And I could not find a way to get the value of the certificate without using replace in the string I get.

    
asked by anonymous 06.06.2018 / 16:43

0 answers