XML Digital Signature in java(JDK1.8) digest generation issue for Document with no Id

38 views Asked by At

I am Working on XML Digital Signature in Java (JDK-1.8).

I am trying to Generate the Digest for the Document.

  <AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01"></AppHdr>
  <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.003.001.07" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.003.001.07 camt.003.001.07.xsd">
    <GetAcct>
      <MsgHdr>
        <MsgId>MTN000004</MsgId>
        <CreDtTm>2021-04-22T15:41:29+02:00</CreDtTm>
      </MsgHdr>
      <AcctQryDef>
        <AcctCrit>
          <NewCrit>
            <SchCrit>
              <AcctId>
                <EQ>
                  <Othr>
                    <Id>250788123456</Id>
                  </Othr>
                </EQ>
              </AcctId>
              <AcctOwnr>
                <CtctDtls>
                  <MobNb>250788123456</MobNb>
                </CtctDtls>
              </AcctOwnr>
            </SchCrit>
          </NewCrit>
        </AcctCrit>
      </AcctQryDef>
    </GetAcct>
  </Document>
</BusinessMessage>

and I am trying the below

Transform transform = signatureFactory.newTransform("http://www.w3.org/2001/10/xml-exc-c14n#",(TransformParameterSpec) null);
Reference reference = signatureFactory.newReference("", signatureFactory.newDigestMethod("http://www.w3.org/2001/04/xmlenc#sha256", null), Arrays.asList(new Transform[]{transform}), null, null);

Here we need to pass the URI to generate the digest, but there is no Id in my Document.

When I have tried to push as a string, It is not generating correctly. Like below

XMLSignatureFactory signatureFactory = XMLSignatureFactory.getInstance("DOM");

        MessageDigest md = MessageDigest.getInstance("SHA-256");

        String msg = "<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:camt.003.001.07\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:iso:std:iso:20022:tech:xsd:camt.003.001.07 camt.003.001.07.xsd\">\n" +
                "  <GetAcct>\n" +
                "    <MsgHdr>\n" +
                "      <MsgId>MTN000004</MsgId>\n" +
                "      <CreDtTm>2021-04-22T15:41:29+02:00</CreDtTm>\n" +
                "    </MsgHdr>\n" +
                "    <AcctQryDef>\n" +
                "      <AcctCrit>\n" +
                "        <NewCrit>\n" +
                "          <SchCrit>\n" +
                "            <AcctId>\n" +
                "              <EQ>\n" +
                "                <Othr>\n" +
                "                  <Id>250788123456</Id>\n" +
                "                </Othr>\n" +
                "              </EQ>\n" +
                "            </AcctId>\n" +
                "            <AcctOwnr>\n" +
                "              <CtctDtls>\n" +
                "                <MobNb>250788123456</MobNb>\n" +
                "              </CtctDtls>\n" +
                "            </AcctOwnr>\n" +
                "          </SchCrit>\n" +
                "        </NewCrit>\n" +
                "      </AcctCrit>\n" +
                "    </AcctQryDef>\n" +
                "  </GetAcct>\n" +
                "</Document>";

        md.update(msg.getBytes());
        byte[] digest = md.digest();

        Transform transform = signatureFactory.newTransform(canonicalizationMethod,(TransformParameterSpec) null);
        Reference reference = signatureFactory.newReference(uri, signatureFactory.newDigestMethod(digestMethod, null),
                Arrays.asList(new Transform[]{transform}), null, null, digest);

How can I create the digest?

Here I was reading the File like below:

Document doc = dbf.newDocumentBuilder().parse(new FileInputStream("D:\\ss\\document1.xml"/*args[0]*/));

According to my partner DigestValue should be 7Ml7CtCRHElRleLjAUXDylAJZhv/WN8Ftst+8d74xMo=

0

There are 0 answers