Example usage for org.bouncycastle.operator DigestCalculator getOutputStream

List of usage examples for org.bouncycastle.operator DigestCalculator getOutputStream

Introduction

In this page you can find the example usage for org.bouncycastle.operator DigestCalculator getOutputStream.

Prototype

OutputStream getOutputStream();

Source Link

Document

Returns a stream that will accept data for the purpose of calculating a digest.

Usage

From source file:it.trento.comune.j4sign.examples.TsdTest.java

License:Open Source License

public boolean validate(String outPath) {

    boolean timestampValid = false;

    DigestCalculatorProvider digestCalculatorProvider = new BcDigestCalculatorProvider();
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    System.out.println("Validating TSD ...");

    try {/*from w  w  w  . jav  a2 s . c om*/

        Streams.pipeAll(cmsTimeStampedData.getContent(), bOut);

        DigestCalculator imprintCalculator = cmsTimeStampedData
                .getMessageImprintDigestCalculator(digestCalculatorProvider);

        Streams.pipeAll(new ByteArrayInputStream(bOut.toByteArray()), imprintCalculator.getOutputStream());

        cmsTimeStampedData.validate(digestCalculatorProvider, imprintCalculator.getDigest());

        timestampValid = true;
        System.out.println("Timestamp validated.");

        System.out.println("Writing extracted data to file: " + outPath);
        FileOutputStream fos = new FileOutputStream(outPath);
        fos.write(bOut.toByteArray());

    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
    } catch (OperatorCreationException e) {
        System.out.println("OperatorCreationException: " + e.getMessage());
    } catch (ImprintDigestInvalidException e) {
        System.out.println("ImprintDigestInvalidException: " + e.getMessage());
    } catch (CMSException e) {
        System.out.println("CMSException: " + e.getMessage());
    }

    return timestampValid;

}

From source file:org.jnotary.crypto.Hasher.java

License:Open Source License

public static byte[] makeHash(ASN1ObjectIdentifier algorithm, byte[] data)
        throws OperatorCreationException, IOException {
    DigestCalculatorProvider calcProvider = new JcaDigestCalculatorProviderBuilder().setProvider("BC").build();
    DigestCalculator calc = calcProvider.get(new AlgorithmIdentifier(algorithm));
    OutputStream stream = null;//from  w  w  w .  ja va2 s. co  m
    try {
        stream = calc.getOutputStream();
        stream.write(data);
    } finally {
        stream.close();
    }
    return calc.getDigest();
}