Example usage for com.itextpdf.text.pdf.security DigestAlgorithms SHA1

List of usage examples for com.itextpdf.text.pdf.security DigestAlgorithms SHA1

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf.security DigestAlgorithms SHA1.

Prototype

String SHA1

To view the source code for com.itextpdf.text.pdf.security DigestAlgorithms SHA1.

Click Source Link

Document

Algorithm available for signatures since PDF 1.3

Usage

From source file:ec.rubrica.pdf.FirmadorPdf.java

License:Open Source License

public byte[] firmar(PrivateKey pk, X509Certificate certificado, String razon, String ubicacion)
        throws IOException {
    try {/*from  ww w .  j  a  v  a2 s.c o m*/
        // Creating the reader and the stamper
        PdfReader reader = new PdfReader(pdf);

        ByteArrayOutputStream signedPdf = new ByteArrayOutputStream();
        PdfStamper stamper = PdfStamper.createSignature(reader, signedPdf, '\0');

        // Creating the appearance
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setReason(razon);
        appearance.setLocation(ubicacion);

        Rectangle pageSize = reader.getPageSize(1);
        Rectangle position = new Rectangle(15, pageSize.getHeight() - 50, 250, pageSize.getHeight());
        appearance.setVisibleSignature(position, 1, "sig");

        // Creating the signature
        ExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA1, null);

        Certificate[] chain = new Certificate[] { certificado };

        MakeSignature.signDetached(appearance, pks, chain, null, null, tsaClient,
                BouncyCastleProvider.PROVIDER_NAME, 0, MakeSignature.CMS);

        return signedPdf.toByteArray();
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:ec.rubrica.pdf.FirmaPDF.java

License:Open Source License

public static byte[] firmar(byte[] pdf, PrivateKey pk, Certificate[] chain, TSAClient tsaClient)
        throws IOException {
    try {/*from w  ww. j  a v  a 2s  .  c o m*/
        // Creating the reader and the stamper
        PdfReader reader = new PdfReader(pdf);
        ByteArrayOutputStream signedPdf = new ByteArrayOutputStream();
        PdfStamper stamper = PdfStamper.createSignature(reader, signedPdf, '\0');

        // Creating the appearance
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setReason("Testing");
        appearance.setLocation("Quito");
        appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");

        // Creating the signature
        PrivateKeySignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA1, null);

        OcspClient ocsp = new OcspClientBouncyCastle();

        MakeSignature.signDetached(appearance, pks, chain, null, ocsp, tsaClient,
                BouncyCastleProvider.PROVIDER_NAME, 0, MakeSignature.CMS);

        return signedPdf.toByteArray();
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}