本文整理汇总了Java中com.itextpdf.text.pdf.PdfSignatureAppearance.getRangeStream方法的典型用法代码示例。如果您正苦于以下问题:Java PdfSignatureAppearance.getRangeStream方法的具体用法?Java PdfSignatureAppearance.getRangeStream怎么用?Java PdfSignatureAppearance.getRangeStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.pdf.PdfSignatureAppearance
的用法示例。
在下文中一共展示了PdfSignatureAppearance.getRangeStream方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doSign
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
public ByteArrayOutputStream doSign(byte[] pdf, Rectangle stampPos, int pageNmbrForStamp) throws IOException, DocumentException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Certificate[] chain = signCert.toArray(new Certificate[0]);
PdfReader reader = new PdfReader(pdf);
ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
PdfStamper stp = PdfStamper.createSignature(reader, byteOS, '\0', null, true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
if (stampPos != null) {
sap.setVisibleSignature(new com.itextpdf.text.Rectangle(stampPos.x, stampPos.y, stampPos.width, stampPos.height), pageNmbrForStamp, null);
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.NAME_AND_DESCRIPTION);
sap.setAcro6Layers(true);
}
// Siganture Appearance
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached"));
log.info("Creating signature with reason: " + ParamValidator.getInstance().getSignatureReason());
sap.setReason(ParamValidator.getInstance().getSignatureReason());
sap.setLocation("Ruhr-Universität Bochum");
Image i = Image.getInstance(getClass().getResource("/de/rub/dez6a3/jpdfsigner/resources/images/sign.png"));
sap.setImage(i);
sap.setCrypto((PrivateKey) signPrivKey, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
dic.setReason(ParamValidator.getInstance().getSignatureReason());
dic.setLocation("Ruhr-Universität Bochum");
sap.setCryptoDictionary(dic);
// preserve some space for the contents
int contentEstimated = 15000;
HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
sap.preClose(exc);
// make the digest
InputStream data = sap.getRangeStream();
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte buf[] = new byte[8192];
int n;
while ((n = data.read(buf)) > 0) {
messageDigest.update(buf, 0, n);
}
byte hash[] = messageDigest.digest();
Calendar cal = Calendar.getInstance();
// If we add a time stamp:
TSAClient tsc = new TSAClientBouncyCastle("http://zeitstempel.dfn.de/");
// Create the signature
PdfPKCS7 sgn;
try {
sgn = new PdfPKCS7((PrivateKey) signPrivKey, chain, null, "SHA1", null, false);
byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, null);
sgn.update(sh, 0, sh.length);
byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, null);
if (contentEstimated + 2 < encodedSig.length) {
throw new DocumentException("Not enough space");
}
byte[] paddedSig = new byte[contentEstimated];
System.arraycopy(encodedSig, 0, paddedSig, 0, encodedSig.length);
// Replace the contents
PdfDictionary dic2 = new PdfDictionary();
dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
sap.close(dic2);
} catch (NoSuchProviderException ex) {
ex.printStackTrace();
}
return byteOS;
}
示例2: preSign
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
public DigestInfo preSign(List<DigestInfo> arg0, List<X509Certificate> certificates)
throws NoSuchAlgorithmException {
System.out.println("SignatureServiceImpl::preSign");
HttpSession session = getSession();
SignatureRequest request = (SignatureRequest)session.getAttribute(BeidConstants.SIGNATUREREQUEST_SESSION_NAME);
ContentStream content = request.getDocument().getContentStream();
try
{
Certificate[] chain = new Certificate[certificates.size()];
int index = 0;
for (X509Certificate cert: certificates)
{
//System.out.println("CERT: "+cert);
chain[index++] = cert;
}
// we create a reader and a stamper
PdfReader reader = new PdfReader(content.getStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper = PdfStamper.createSignature(reader, baos, '\0');
// we create the signature appearance
PdfSignatureAppearance sap = stamper.getSignatureAppearance();
request.fillAppearance(sap, reader);
sap.setCertificate(chain[0]);
// we create the signature infrastructure
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
dic.setReason(sap.getReason());
dic.setLocation(sap.getLocation());
dic.setContact(sap.getContact());
dic.setDate(new PdfDate(sap.getSignDate()));
sap.setCryptoDictionary(dic);
HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
exc.put(PdfName.CONTENTS, new Integer(8192 * 2 + 2));
sap.preClose(exc);
ExternalDigest externalDigest = new ExternalDigest() {
public MessageDigest getMessageDigest(String hashAlgorithm)
throws GeneralSecurityException {
return DigestAlgorithms.getMessageDigest(hashAlgorithm,
null);
}
};
PdfPKCS7 sgn = new PdfPKCS7(null, chain, "SHA256", null, externalDigest, false);
InputStream data = sap.getRangeStream();
byte hash[] = DigestAlgorithms.digest(data, externalDigest.getMessageDigest("SHA256"));
Calendar cal = Calendar.getInstance();
byte[] sh = sgn.getAuthenticatedAttributeBytes(hash, cal, null, null, CryptoStandard.CMS);
sh = MessageDigest.getInstance("SHA256", "BC").digest(sh);
// We store the objects we'll need for post signing in a session
session.setAttribute(BeidConstants.SIGNATURE_SESSION_NAME, sgn);
session.setAttribute(BeidConstants.HASH_SESSION_NAME, hash);
session.setAttribute(BeidConstants.CAL_SESSION_NAME, cal);
session.setAttribute(BeidConstants.SAP_SESSION_NAME, sap);
session.setAttribute(BeidConstants.BAOS_SESSION_NAME, baos);
DigestInfo info = new DigestInfo(sh, "SHA-256", "BeidSign");
return info;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}