/** * Provides the payload of a KDBX file as an unencrypted {@link InputStream}. * @param credentials credentials for decryption of the stream * @param kdbxHeader a header instance to be populated with values from the stream * @param inputStream a KDBX formatted input stream * @return an unencrypted input stream, to be read and closed by the caller * @throws IOException on error */ public static InputStream createUnencryptedInputStream(Credentials credentials, KdbxHeader kdbxHeader, InputStream inputStream) throws IOException { readKdbxHeader(kdbxHeader, inputStream); InputStream decryptedInputStream = kdbxHeader.createDecryptedStream(credentials.getKey(), inputStream); checkStartBytes(kdbxHeader, decryptedInputStream); HashedBlockInputStream blockInputStream = new HashedBlockInputStream(decryptedInputStream, true); if (kdbxHeader.getCompressionFlags().equals(KdbxHeader.CompressionFlags.NONE)) { return blockInputStream; } return new GZIPInputStream(blockInputStream); }
/** * Provides an {@link OutputStream} to be encoded and encrypted in KDBX format * @param credentials credentials for encryption of the stream * @param kdbxHeader a KDBX header to control the formatting and encryption operation * @param outputStream output stream to contain the KDBX formatted output * @return an unencrypted output stream, to be written to, flushed and closed by the caller * @throws IOException on error */ public static OutputStream createEncryptedOutputStream(Credentials credentials, KdbxHeader kdbxHeader, OutputStream outputStream) throws IOException { writeKdbxHeader(kdbxHeader, outputStream); OutputStream encryptedOutputStream = kdbxHeader.createEncryptedStream(credentials.getKey(), outputStream); writeStartBytes(kdbxHeader, encryptedOutputStream); HashedBlockOutputStream blockOutputStream = new HashedBlockOutputStream(encryptedOutputStream, true); if(kdbxHeader.getCompressionFlags().equals(KdbxHeader.CompressionFlags.NONE)) { return blockOutputStream; } return new GZIPOutputStream(blockOutputStream); }
InputStream decryptedInputStream = kdbHeader.createDecryptedInputStream(credentials.getKey(), inputStream);