Hi! I'm working to an asynchronous digital signature service and now i'd like to add pdf capabilities. I need a way to make the hashing process repeatable: if i try to insert a signature in a pdf document the hash of the signable part should be the same doesn't matter how many times i invoke the method on the same source document. This is already true for invisible signatures, but when i try to add a visible signature the hash becomes different every time i invoke the hashing method. I've been hunting some static field in iText, such as the one contained in Image, but it's still not working. Can you give me some advice? Are there other methods? for instance what about preparing a blank signature? (i'm new to pdf, sorry!) Any help would be appreciated!
Gabriele Contini import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import com.lowagie.text.Image; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.ByteBuffer; import com.lowagie.text.pdf.PdfDate; import com.lowagie.text.pdf.PdfDictionary; import com.lowagie.text.pdf.PdfName; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfSignatureAppearance; import com.lowagie.text.pdf.PdfStamper; import com.lowagie.text.pdf.PdfString; public class MainScrapbook { public static void main(String[] args) throws Exception { Date date = new Date(1000); String layer2text = "l2text"; byte[] document = toByteArray(MainScrapbook.class .getResourceAsStream("HelloWorld.pdf")); // first call to the hash function Image.serialId = 0; ByteBuffer.setCacheSize(0); byte[] hash = calcolaHash(document, date, layer2text); System.out.println(compactHexify(hash)); // reset some static data and try a second call... Image.serialId = 0; ByteBuffer.setCacheSize(0); hash = calcolaHash(document, date, layer2text); System.out.println(compactHexify(hash)); // the two hashes are different :-( } private static byte[] calcolaHash(byte[] docBytes, Date signDate, String l2text) throws Exception { int nbytes = docBytes.length; PdfReader reader = new PdfReader(docBytes); ByteArrayOutputStream foo = new ByteArrayOutputStream(); PdfStamper stp = PdfStamper.createSignature(reader, foo, '\0'); PdfSignatureAppearance sap = stp.getSignatureAppearance(); sap.setLayer2Text(l2text); sap.setVisibleSignature(new Rectangle(128, 128, 256, 256), 1, "SIG"); Calendar cal = Calendar.getInstance(); cal.setTime(signDate); PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.FT, PdfName.SIG); dic.put(PdfName.FILTER, PdfName.ADOBE_PPKMS); dic.put(PdfName.SUBFILTER, PdfName.ADBE_PKCS7_SHA1); dic.put(PdfName.M, new PdfDate(cal)); dic.put(PdfName.NAME, new PdfString("test")); dic.put(PdfName.REASON, new PdfString("test")); sap.setCryptoDictionary(dic); HashMap exc = new HashMap(); exc.put(PdfName.CONTENTS, new Integer(nbytes * 2 + 2)); sap.preClose(exc); MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); InputStream inp = sap.getRangeStream(); byte[] bytes = toByteArray(inp); messageDigest.update(bytes); byte hash[] = messageDigest.digest(); return hash; } private static String compactHexify(byte[] data) { StringBuffer out = new StringBuffer(256); for (int i = 0; i < data.length; i++) { out.append(Integer.toHexString(0xff & data[i])); } return out.toString(); } private static byte[] toByteArray(InputStream istream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buffer = new byte[8192]; int n = 0; while (-1 != (n = istream.read(buffer))) { baos.write(buffer, 0, n); } return baos.toByteArray(); } } ------------------------------------------------------------------------- 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/