Created
October 15, 2014 18:16
-
-
Save alexbartlow/b520517d7ebdd400682e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The signing implementation | |
The SignImp class isn’t much different from all the examples we’ve written before. When I write | |
Servlets that involve the use of iText, I always start by writing a short standalone application with a | |
main() method. This way I can test the code before integrating it into a web application. | |
In code sample 4.11, I’ve removed the main() method for brevity. | |
Code sample 4.11: the SignImp class | |
public class SignImp { | |
private PrivateKey pk; | |
private Certificate[] chain; | |
public SignImp(PrivateKey pk, Certificate[] chain) { | |
this.pk = pk; | |
this.chain = chain; | |
} | |
public byte[] signDoc(InputStream pdf, String reason, String location) | |
throws GeneralSecurityException, IOException, DocumentException { | |
// Creating the reader and the stamper | |
PdfReader reader = new PdfReader(pdf, null); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
PdfStamper stamper = | |
PdfStamper.createSignature(reader, baos, '\0', null, true); | |
// Creating the appearance | |
PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); | |
appearance.setReason(reason); | |
appearance.setLocation(location); | |
// Creating the signature | |
ExternalSignature pks = | |
new PrivateKeySignature(pk, DigestAlgorithms.SHA256, null); | |
ExternalDigest digest = new BouncyCastleDigest(); | |
MakeSignature.signDetached(appearance, digest, pks, chain, | |
null, null, null, 0, CryptoStandard.CMS); | |
return baos.toByteArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment