List of usage examples for org.bouncycastle.crypto.io DigestOutputStream DigestOutputStream
public DigestOutputStream(Digest Digest)
From source file:com.joyent.manta.http.entity.DigestedEntity.java
License:Open Source License
@Override public void writeTo(final OutputStream out) throws IOException { digest.reset(); // reset the digest state in case we're in a retry // If our wrapped entity is backed by a buffer of some form // we can read easily read the whole buffer into our message digest. if (wrapped instanceof MemoryBackedEntity) { final MemoryBackedEntity entity = (MemoryBackedEntity) wrapped; final ByteBuffer backingBuffer = entity.getBackingBuffer(); if (backingBuffer.hasArray()) { final byte[] bytes = backingBuffer.array(); final int offset = backingBuffer.arrayOffset(); final int position = backingBuffer.position(); final int limit = backingBuffer.limit(); digest.update(bytes, offset + position, limit - position); backingBuffer.position(limit); wrapped.writeTo(out);//from w w w. j av a2s . c o m } } else { try (DigestOutputStream dout = new DigestOutputStream(digest); TeeOutputStream teeOut = new TeeOutputStream(out, dout)) { wrapped.writeTo(teeOut); teeOut.flush(); } } }
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 ww w . j a v a 2 s .c o 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 w w w . j a v a 2 s.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(); }
From source file:edu.wisc.doit.tcrypt.BouncyCastleFileEncrypter.java
License:Apache License
@Override public OutputStream encrypt(String fileName, int size, OutputStream outputStream) throws InvalidCipherTextException, IOException, DecoderException { final TarArchiveOutputStream tarOutputStream = new TarArchiveOutputStream(outputStream, ENCODING); final BufferedBlockCipher cipher = createCipher(tarOutputStream); startEncryptedFile(fileName, size, tarOutputStream, cipher); //Protect the underlying TAR stream from being closed by the cipher stream final CloseShieldOutputStream closeShieldTarStream = new CloseShieldOutputStream(tarOutputStream); //Setup the encrypting cipher stream final CipherOutputStream chipherStream = new CipherOutputStream(closeShieldTarStream, cipher); //Generate a digest of the pre-encryption data final GeneralDigest digest = this.createDigester(); final DigestOutputStream digestOutputStream = new DigestOutputStream(digest); //Write data to both the digester and cipher final TeeOutputStream teeStream = new TeeOutputStream(digestOutputStream, chipherStream); return new EncryptingOutputStream(teeStream, tarOutputStream, digest); }
From source file:io.netty.example.ocsp.Digester.java
License:Apache License
private Digester(Digest digest, AlgorithmIdentifier algId) { this.dos = new DigestOutputStream(digest); this.algId = algId; }