Example usage for org.bouncycastle.operator DigestCalculator getDigest

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

Introduction

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

Prototype

byte[] getDigest();

Source Link

Document

Return the digest calculated on what has been written to the calculator's output stream.

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.j av  a 2 s . co  m*/

        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  v  a 2s .  c  om*/
    try {
        stream = calc.getOutputStream();
        stream.write(data);
    } finally {
        stream.close();
    }
    return calc.getDigest();
}