6 Replies Latest reply: Mar 22, 2012 2:10 PM by 923432 RSS

    Xml Signature

    923432
      HI ,
      i can sign xml document with this lib http://docs.oracle.com/javase/6/docs/technotes/guides/security/xmldsig/XMLDigitalSignature.html ;

      but how i can sign other type files ( example .doc, .pdf or more) and result in xml file

      i can convert this files to byte strign and i create a xml doc and add that byte string to xml , it is possible and is signed successfully , but when that type files (docx, pdf) more than 6 mb then error occured; out of memory exceptions;
      Do you have other idea for sign doc. pdf file with xml dig sign

      thanks
        • 1. Re: Xml Signature
          EJP
          You can't sign anything except an XML file with an XML digital signature, at least not with the standard Java API.

          If you need more memory to convert to XML, get more memory, or smaller files, or run the conversion in a separate JVM.
          • 2. Re: Xml Signature
            923432
            thanks for you answer , hmm
            • 3. Re: Xml Signature
              smullan
              EJP wrote:
              You can't sign anything except an XML file with an XML digital signature, at least not with the standard Java API.
              That's not actually correct. You can sign non-XML data with an XML Signature using the javax.xml.crypto API. You can sign the binary content fetched from an http or a file URI or write your own URIDereferencer to do the same.

              >
              If you need more memory to convert to XML, get more memory, or smaller files, or run the conversion in a separate JVM.
              • 4. Re: Xml Signature
                923432
                Thanks ;

                it is possible parse xml string and xml document and sign

                Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlStream)));
                DOMSignContext dsc = new DOMSignContext((PrivateKey)kp, doc.getDocumentElement());

                     XMLSignature signature = fac.newXMLSignature(si, ki);
                     signature.sign(dsc);

                so , how i can sing binary document , can you send an example ?
                • 5. Re: Xml Signature
                  smullan
                  920429 wrote:
                  Thanks ;

                  it is possible parse xml string and xml document and sign

                  Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlStream)));
                  DOMSignContext dsc = new DOMSignContext((PrivateKey)kp, doc.getDocumentElement());

                       XMLSignature signature = fac.newXMLSignature(si, ki);
                       signature.sign(dsc);

                  so , how i can sing binary document , can you send an example ?
                  Assume your binary data is stored in a file, simply create a reference with a file URI pointing to that file:

                  Reference ref = fac.newReference("file:<path-to-file>", fac.newDigestMethod(DigestMethod.SHA1, null));

                  Alternatively, you could base64-encode the binary data, and embed it inside an XML document, but I don't have code available to show you how to do that.
                  • 6. Re: Xml Signature
                    923432
                    thanks

                    signature.sign(dsc);
                    so , now
                    The cause of the problem is the use of a URI-type reference: if I use a "file:/..." one, Exception occurs, if I use a "http://..." one, then there is no Exception

                    Reference ref_1 = xsfac.newReference("file:/C:/temp/myPix.jpg",xsfac.newDigestMethod(DigestMethod.SHA1, null));
                    Reference ref_2 = xsfac.newReference("file:///C:/temp/myPix.jpg",xsfac.newDigestMethod(DigestMethod.SHA1, null));
                    Reference ref_3 = xsfac.newReference("http://www.mycompany.com/data/myPix.jpg", xsfac.newDigestMEthod(DigestMethod.SHA1,null));



                    it is same exception with this:
                    javax.xml.crypto.URIReferenceException:

                    can anybody solve this problem ?
                    thanks

                    Edited by: 920429 on Mar 22, 2012 12:07 PM