rikulo / xml-crypto

Xml digital signature library for Dart
https://pub.dev/packages/xml_crypto
MIT License
4 stars 0 forks source link

Incorret digest and signature value #6

Closed pedroserretti closed 8 months ago

pedroserretti commented 8 months ago

Hi, i'm getting an error in xml signing

          SignedXml sig = SignedXml();
          sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
          sig.canonicalizationAlgorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";

          for (XmlAttribute atributo in tagElements.first.attributes) {
            if (atributo.name.local == 'Id') {
              sig.addReference("//*[local-name()='infNFe']", ["http://www.w3.org/2000/09/xmldsig#enveloped-signature", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"], "http://www.w3.org/2000/09/xmldsig#sha1", "#${atributo.value}");
            }
          }

          Directory tempDir = await getTemporaryDirectory();
          File tempFile = File('${tempDir.path}\\chave_privada.pem');
          tempFile.writeAsStringSync(sChave);

          sig.signingKey = chaveBytes;
          sig.keyInfoProvider = FileKeyInfo(tempFile.path);
          sig.computeSignature(xmlString);
          XmlDocument newDocument = XmlDocument.parse(sig.signedXml);
          await File(arquivoXml).writeAsString(newDocument.toXmlString(pretty: true));

I'm trying to sign my XML, but digestValue and signatureValue are incorrects, i'm doing the same function in Csharp to signing, and generate it correctly. Can anyone help me, please?

scribetw commented 8 months ago

Hi,

Please try using the original Node.js version of xml-crypto@3.2.0 to see if the result is correct.

If correct, the Dart porting may have some bugs. Or the bug was from the original implementation.

Anyway, it's better if you can provide the sample XML file for further debugging.

pedroserretti commented 8 months ago

Hi,

Please try using the original Node.js version of xml-crypto@3.2.0 to see if the result is correct.

If correct, the Dart porting may have some bugs. Or the bug was from the original implementation.

Anyway, it's better if you can provide the sample XML file for further debugging.

Actually, i needed to remove all whitespaces before sign my xml. To resolve, i added document.normalize(trimAllWhitespaces: true) and my problem was solved.