Hi

I have a pdf to sign and external signature.
When I send completed array bytes to sign, I get correct signature but when
I send hash of bytes I get a
"The document has been altered or corrupted since the Signature was applied"

My code is:

            PdfReader reader = new PdfReader(thePDF);
            FileOutputStream fout = new FileOutputStream(new
File("\\cert_prova_sig1.pdf"));
            PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
            Calendar now = Calendar.getInstance();
            PdfSignatureAppearance sap = stp.getSignatureAppearance();
            System.out.println("creating visible signature ...");
            String reason = "reason";
            String location = "location";
            sap.setReason(reason);
            sap.setLocation(location);
            sap.setVisibleSignature(new Rectangle(100, 100, 186, 130), 1, null);

            StringBuffer buf = new StringBuffer();
            buf.append("Signed by ").append(" *******").append('\n');
            sap.setLayer2Text(buf.toString());
            sap.setAcro6Layers(true);
            PdfDictionary dic = new PdfDictionary();
            dic.put(PdfName.TYPE, PdfName.SIG);
            dic.put(PdfName.FILTER, PdfName.ADOBE_PPKLITE);
            dic.put(PdfName.SUBFILTER, PdfName.ADBE_PKCS7_DETACHED);
            dic.put(PdfName.M, new PdfDate(now));
            dic.put(PdfName.REASON, new PdfString(reason));
            dic.put(PdfName.LOCATION, new PdfString(location));
            System.out.println("setting the dictionary to the appearance...");
            sap.setCryptoDictionary(dic);

            HashMap exc = new HashMap();
            // Espai a reservar, depenent de la signatura...
            Integer reservedSpace = new Integer(0x6502);
            exc.put(PdfName.CONTENTS, reservedSpace);
            System.out.println("preclosing the signature appearance");
            sap.setExternalDigest(new byte[128], new byte[20], null);
            sap.preClose(exc);

            byte[] hashREal = MessageDigest.getInstance("SHA1",
"BC").digest((Utils.streamToByteArray(sap.getRangeStream())));

            byte[] churroBytes = 
(Utils.streamToByteArray(sap.getRangeStream()));
          
           // if signed with sap.getRangeStream is ok
            //      byte[] signat = signerCaja.signDataDettached(churroBytes);
           // but when I signed with hash, I get error in verifiy signature
            byte[] signat = signerCaja.signDataDettached(hashREal);
            assertNotNull(signat);
            File resp = new File("\\signaturaPREDettached.p7s");
            resp.delete();
            new FileOutputStream(resp).getFD().sync();
            resp.createNewFile();
            FileUtils.writeByteArrayToFile(resp, signat);
            byte sg[] = signat;

            // byte sg[] = theSignature;
            byte[] out = new byte[(reservedSpace.intValue() - 2) / 2];
            System.out.println("Sg Length : " + sg.length + " Out Length : " +
out.length);
            System.arraycopy(sg, 0, out, 0, sg.length);
            PdfDictionary dic2 = new PdfDictionary();
            dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true));
            sap.close(dic2);
            System.out.println("complete");




-- 
View this message in context: 
http://www.nabble.com/Signing-pdf-hash-with-external-signature-tp22912200p22912200.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to