List of usage examples for org.apache.pdfbox.pdmodel.interactive.digitalsignature SignatureInterface SignatureInterface
SignatureInterface
From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxSignatureService.java
License:Open Source License
private byte[] signDocumentAndReturnDigest(final SignatureParameters parameters, final byte[] signatureBytes, final File signedFile, final FileOutputStream fileOutputStream, final PDDocument pdDocument, final PDSignature pdSignature, final DigestAlgorithm digestAlgorithm) throws DSSException { try {/*from w w w.j ava 2s . c o m*/ final MessageDigest digest = DSSUtils.getMessageDigest(digestAlgorithm); // register signature dictionary and sign interface SignatureInterface signatureInterface = new SignatureInterface() { @Override public byte[] sign(InputStream content) throws SignatureException, IOException { byte[] b = new byte[4096]; int count; while ((count = content.read(b)) > 0) { digest.update(b, 0, count); } return signatureBytes; } }; pdDocument.addSignature(pdSignature, signatureInterface); saveDocumentIncrementally(parameters, signedFile, fileOutputStream, pdDocument); final byte[] digestValue = digest.digest(); if (LOG.isDebugEnabled()) { LOG.debug("Digest to be signed: " + DSSUtils.encodeHexString(digestValue)); } fileOutputStream.close(); return digestValue; } catch (NoSuchAlgorithmException e) { throw new DSSException(e); } catch (IOException e) { throw new DSSException(e); } catch (SignatureException e) { throw new DSSException(e); } }
From source file:eu.europa.esig.dss.pdf.pdfbox.PdfBoxSignatureService.java
License:Open Source License
private byte[] signDocumentAndReturnDigest(final PAdESSignatureParameters parameters, final byte[] signatureBytes, final File signedFile, final FileOutputStream fileOutputStream, final PDDocument pdDocument, final PDSignature pdSignature, final DigestAlgorithm digestAlgorithm) throws DSSException { SignatureOptions options = new SignatureOptions(); try {//from ww w. j ava 2 s . c om final MessageDigest digest = DSSUtils.getMessageDigest(digestAlgorithm); // register signature dictionary and sign interface SignatureInterface signatureInterface = new SignatureInterface() { @Override public byte[] sign(InputStream content) throws SignatureException, IOException { byte[] b = new byte[4096]; int count; while ((count = content.read(b)) > 0) { digest.update(b, 0, count); } return signatureBytes; } }; options.setPreferedSignatureSize(parameters.getSignatureSize()); if (parameters.getImageParameters() != null) { fillImageParameters(pdDocument, parameters.getImageParameters(), options); } pdDocument.addSignature(pdSignature, signatureInterface, options); saveDocumentIncrementally(parameters, signedFile, fileOutputStream, pdDocument); final byte[] digestValue = digest.digest(); if (logger.isDebugEnabled()) { logger.debug("Digest to be signed: " + Hex.encodeHexString(digestValue)); } fileOutputStream.close(); return digestValue; } catch (IOException e) { throw new DSSException(e); } catch (SignatureException e) { throw new DSSException(e); } finally { IOUtils.closeQuietly(options.getVisualSignature()); } }