Hello everyone,
joy of joys, its working!

Sorry for double posting, but just wanted to put what worked for me here.

            try{
                // Initializes the Citizens Card
                CcMethods.init();
                
                // Creates the test files and gets their streams
                        File outFile = new File("C:/signed.pdf");
                FileOutputStream os = new FileOutputStream(outFile);
                        
                // Creates a reader, gets the first page and creates a signature
                PdfReader reader = new PdfReader("C:/unsigned.pdf");
                PdfStamper stamp = PdfStamper.createSignature(reader, os, '\0');
                PdfSignatureAppearance appearance = 
stamp.getSignatureAppearance();
                
                // Sets the signature visible and on the front page (top right
corner)
                Rectangle rect = reader.getPageSize(1); 
                appearance.setVisibleSignature(new Rectangle(rect.getWidth() - 
82,
rect.getHeight()- 78, rect.getWidth() -10, rect.getHeight() -30), 1, "sig");
                
                // Sets crypto parameters with the proper certificates, wincer 
is
advised
                appearance.setCrypto(null, CcMethods.getChain(), null,
PdfSignatureAppearance.VERISIGN_SIGNED);
                
                // Additional properties in the signing process 
                appearance.setReason("Autenticação do documento.");
//              appearance.setLocation("Name of the location");
//              appearance.setContact("Some sort of contact");
//              appearance.setSignDate(new GregorianCalendar());
                // Allocates space for the signed hash/digest
                appearance.setExternalDigest(new byte[128], null, "RSA");
                appearance.preClose();

                // Process of hash/digest generation
                    MessageDigest md = MessageDigest.getInstance("SHA-1");
                    byte buf[] = new byte[8192];
                    int n = 0;
                    InputStream rs = appearance.getRangeStream();
                    md.reset();
                    while ((n = rs.read(buf)) != -1)
                        md.update(buf);
                    rs.close();
                byte[] hashDig = md.digest();
                // Signs the hash
                byte[] signedHash = CcMethods.sign(hashDig);
                
                // Places the signed hash/digest in the document
                if(signedHash != null){
                        PdfDictionary dic = new PdfDictionary();
                                PdfPKCS7 sig = 
appearance.getSigStandard().getSigner();
                        sig.setExternalDigest(signedHash, null, "RSA");
                        dic.put(PdfName.CONTENTS, new
PdfString(sig.getEncodedPKCS1()).setHexWriting(true));
                        appearance.close(dic);
                }
                else
                        System.out.println("Hash not signed");
                
                CcMethods.finish();
                os.close();
                }
                catch (Exception e){
                        e.printStackTrace();
                }


Thanks Andreas and Michael for your help.
Regards,
Pedro
-- 
View this message in context: 
http://itext-general.2136553.n4.nabble.com/Another-invalid-signature-thread-tp3003710p3013653.html
Sent from the iText - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to