List of usage examples for com.itextpdf.text.pdf PdfName ETSI_RFC3161
PdfName ETSI_RFC3161
To view the source code for com.itextpdf.text.pdf PdfName ETSI_RFC3161.
Click Source Link
From source file:com.swisscom.ais.itext.PDF.java
License:Open Source License
/** * Add signature information (reason for signing, location, contact, date) and create hash from pdf document * * @param signDate Date of signing * @param estimatedSize The estimated size for signatures * @param hashAlgorithm The hash algorithm which will be used to sign the pdf * @param isTimestampOnly If it is a timestamp signature. This is necessary because the filter is an other one compared to a "standard" signature * @return Hash of pdf as bytes//from w w w .j a v a 2 s . c om * @throws Exception */ public byte[] getPdfHash(@Nonnull Calendar signDate, int estimatedSize, @Nonnull String hashAlgorithm, boolean isTimestampOnly) throws Exception { pdfReader = new PdfReader(inputFilePath, pdfPassword != null ? pdfPassword.getBytes() : null); AcroFields acroFields = pdfReader.getAcroFields(); boolean hasSignature = acroFields.getSignatureNames().size() > 0; byteArrayOutputStream = new ByteArrayOutputStream(); pdfStamper = PdfStamper.createSignature(pdfReader, byteArrayOutputStream, '\0', null, hasSignature); pdfStamper.setXmpMetadata(pdfReader.getMetadata()); pdfSignatureAppearance = pdfStamper.getSignatureAppearance(); pdfSignature = new PdfSignature(PdfName.ADOBE_PPKLITE, isTimestampOnly ? PdfName.ETSI_RFC3161 : PdfName.ADBE_PKCS7_DETACHED); pdfSignature.setReason(signReason); pdfSignature.setLocation(signLocation); pdfSignature.setContact(signContact); pdfSignature.setDate(new PdfDate(signDate)); pdfSignatureAppearance.setCryptoDictionary(pdfSignature); // certify the pdf, if requested if (certificationLevel > 0) { // check: at most one certification per pdf is allowed if (pdfReader.getCertificationLevel() != PdfSignatureAppearance.NOT_CERTIFIED) throw new Exception( "Could not apply -certlevel option. At most one certification per pdf is allowed, but source pdf contained already a certification."); pdfSignatureAppearance.setCertificationLevel(certificationLevel); } HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>(); exc.put(PdfName.CONTENTS, new Integer(estimatedSize * 2 + 2)); pdfSignatureAppearance.preClose(exc); MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm); InputStream rangeStream = pdfSignatureAppearance.getRangeStream(); int i; while ((i = rangeStream.read()) != -1) { messageDigest.update((byte) i); } return messageDigest.digest(); }