It's not posible to predict the signature size. Sign twice, the first time 
to know the size.

Paulo

----- Original Message ----- 
From: "Armin Häberling" <[EMAIL PROTECTED]>
To: <itext-questions@lists.sourceforge.net>
Sent: Monday, December 11, 2006 5:08 PM
Subject: [iText-questions] computing the signature size before signing?


> Hi,
>
> Is there a convenience way to compute the size needed for an external
> signature, prior to signing?
> To clarify my question. The following is an example from
> http://itextpdf.sourceforge.net/howtosign.html, which shows how an
> external signature is added to a pdf.
>
>> KeyStore ks = KeyStore.getInstance("pkcs12");
>> ks.load(new FileInputStream("my_private_key.pfx"),
>> "my_password".toCharArray());
>> String alias = (String)ks.aliases().nextElement();
>> PrivateKey key = (PrivateKey)ks.getKey(alias, "password".toCharArray());
>> Certificate[] chain = ks.getCertificateChain(alias);
>>
>> PdfReader reader = new PdfReader("original.pdf");
>> FileOutputStream fout = new FileOutputStream("signed.pdf");
>> PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
>> PdfSignatureAppearance sap = stp.getSignatureAppearance();
>> // comment next line to have an invisible signature
>> sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, null);
>> sap.setLayer2Text("This is some custom made text.\n\nDate: some date");
>> Calendar cal = Calendar.getInstance();
>> PdfDictionary dic = new PdfDictionary();
>> dic.put(PdfName.FT, PdfName.SIG);
>> dic.put(PdfName.FILTER, new PdfName("SAFE.PPKSF"));
>> dic.put(PdfName.SUBFILTER, new PdfName("adbe.pkcs7.detached"));
>> dic.put(PdfName.M, new PdfDate(cal));
>> dic.put(PdfName.NAME, new
>> PdfString(PdfPKCS7.getSubjectFields((X509Certificate)chain[0]).getField("CN")));
>> sap.setCryptoDictionary(dic);
>> HashMap exc = new HashMap();
>
>> exc.put(PdfName.CONTENTS, new Integer(0x2502));
>
> here we save space for the signature. the size of the signature is
> hardcoded in this example. my question is how do I compute this size
> prior to generating the signature?
>
>> sap.preClose(exc);
>>
>> PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null, "SHA1", null, false);
>> MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
>> byte buf[] = new byte[8192];
>> int n;
>> InputStream inp = sap.getRangeStream();
>> while ((n = inp.read(buf)) > 0) {
>>     messageDigest.update(buf, 0, n);
>> }
>> byte hash[] = messageDigest.digest();
>> byte sh[] = pk7.getAuthenticatedAttributeBytes(hash, cal);
>> pk7.update(sh, 0, sh.length);
>> PdfDictionary dic2 = new PdfDictionary();
>> byte sg[] = pk7.getEncodedPKCS7(hash, cal);
>> byte out[] = new byte[0x2500 / 2];
>> System.arraycopy(sg, 0, out, 0, sg.length);
>> dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true));
>> sap.close(dic2);
>
> Thanks in advance.
>
> Regards,
> Armin
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share 
> your
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> iText-questions mailing list
> iText-questions@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> Buy the iText book: http://itext.ugent.be/itext-in-action/ 


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to