Example usage for org.bouncycastle.crypto.io DigestOutputStream getDigest

List of usage examples for org.bouncycastle.crypto.io DigestOutputStream getDigest

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.io DigestOutputStream getDigest.

Prototype

public byte[] getDigest() 

Source Link

Usage

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileDecrypter.java

License:Apache License

@Override
public void decrypt(InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException, DecoderException {
    final TarArchiveInputStream tarInputStream = new TarArchiveInputStream(inputStream, FileEncrypter.ENCODING);

    final BufferedBlockCipher cipher = createCipher(tarInputStream);

    //Advance to the next entry in the tar file
    tarInputStream.getNextTarEntry();//from w w  w  .j  a  v a 2 s  .co m

    //Create digest output stream used to generate digest while decrypting
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Do a streaming decryption of the file output
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new TeeOutputStream(outputStream, digestOutputStream), cipher);
    IOUtils.copy(tarInputStream, cipherOutputStream);
    cipherOutputStream.close();

    //Capture the hash of the decrypted output
    final byte[] hashBytes = digestOutputStream.getDigest();
    verifyOutputHash(tarInputStream, hashBytes);
}

From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java

License:Apache License

@Override
public void encrypt(String fileName, int size, InputStream inputStream, OutputStream outputStream)
        throws InvalidCipherTextException, IOException {
    final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream(outputStream, 512,
            ENCODING);/*from ww  w  . j av  a 2s .  com*/

    final BufferedBlockCipher cipher = createCipher(tarArchiveOutputStream);

    startEncryptedFile(fileName, size, tarArchiveOutputStream, cipher);

    //Setup cipher output stream, has to protect from close as the cipher stream must close but the tar cannot get closed yet
    final CipherOutputStream cipherOutputStream = new CipherOutputStream(
            new CloseShieldOutputStream(tarArchiveOutputStream), cipher);

    //Setup digester
    final DigestOutputStream digestOutputStream = new DigestOutputStream(this.createDigester());

    //Perform streaming encryption and hashing of the file
    IOUtils.copy(inputStream, new TeeOutputStream(cipherOutputStream, digestOutputStream));
    cipherOutputStream.close();

    tarArchiveOutputStream.closeArchiveEntry();

    //Capture the hash code of the encrypted file
    digestOutputStream.close();
    final byte[] hashBytes = digestOutputStream.getDigest();

    this.writeHashfile(tarArchiveOutputStream, hashBytes);

    //Close the TAR stream, nothing else should be written to it
    tarArchiveOutputStream.close();
}