Example usage for java.security DigestOutputStream DigestOutputStream

List of usage examples for java.security DigestOutputStream DigestOutputStream

Introduction

In this page you can find the example usage for java.security DigestOutputStream DigestOutputStream.

Prototype

public DigestOutputStream(OutputStream stream, MessageDigest digest) 

Source Link

Document

Creates a digest output stream, using the specified output stream and message digest.

Usage

From source file:ro.kuberam.libs.java.crypto.CryptoModuleTests.java

@Test
public void digestOutputStreamTest() throws Exception {
    final MessageDigest md = MessageDigest.getInstance("SHA-512");

    try (final OutputStream fos = Files.newOutputStream(Paths
            .get("/home/claudius/workspace-claudius/expath-crypto/src/org/expath/crypto/tests/string.txt"));
            final DigestOutputStream dos = new DigestOutputStream(fos, md);
            final ObjectOutputStream oos = new ObjectOutputStream(dos)) {

        final String data = "This have I thought good to deliver thee, "
                + "that thou mightst not lose the dues of rejoicing "
                + "by being ignorant of what greatness is promised thee.";
        oos.writeObject(data);//  w  w w .j a  v  a 2s  . c om
        dos.on(false);
        final byte[] digest = md.digest();
        oos.writeObject(digest);
        final int digestLength = digest.length;
        System.out.println("length: " + digestLength);
        final BigInteger bi = new BigInteger(1, digest);
        String result = bi.toString(digestLength);
        if (result.length() % 2 != 0) {
            result = "0" + result;
        }

        System.out.println("result: " + result);
    }
}