Search in sources :

Example 1 with HashedBlockOutputStream

use of com.keepassdroid.stream.HashedBlockOutputStream in project KeePassDX by Kunzisoft.

the class HashedBlock method testSize.

private void testSize(int blockSize, int bufferSize) throws IOException {
    byte[] orig = new byte[blockSize];
    rand.nextBytes(orig);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    HashedBlockOutputStream output = new HashedBlockOutputStream(bos, bufferSize);
    output.write(orig);
    output.close();
    byte[] encoded = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
    HashedBlockInputStream input = new HashedBlockInputStream(bis);
    ByteArrayOutputStream decoded = new ByteArrayOutputStream();
    while (true) {
        byte[] buf = new byte[1024];
        int read = input.read(buf);
        if (read == -1) {
            break;
        }
        decoded.write(buf, 0, read);
    }
    byte[] out = decoded.toByteArray();
    assertArrayEquals(orig, out);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream) HashedBlockInputStream(com.keepassdroid.stream.HashedBlockInputStream)

Example 2 with HashedBlockOutputStream

use of com.keepassdroid.stream.HashedBlockOutputStream in project KeePassDX by Kunzisoft.

the class PwDbV4Output method output.

@Override
public void output() throws PwDbOutputException {
    try {
        try {
            engine = CipherFactory.getInstance(mPM.dataCipher);
        } catch (NoSuchAlgorithmException e) {
            throw new PwDbOutputException("No such cipher", e);
        }
        header = (PwDbHeaderV4) outputHeader(mOS);
        OutputStream osPlain;
        if (header.version < PwDbHeaderV4.FILE_VERSION_32_4) {
            CipherOutputStream cos = attachStreamEncryptor(header, mOS);
            cos.write(header.streamStartBytes);
            HashedBlockOutputStream hashed = new HashedBlockOutputStream(cos);
            osPlain = hashed;
        } else {
            mOS.write(hashOfHeader);
            mOS.write(headerHmac);
            HmacBlockOutputStream hbos = new HmacBlockOutputStream(mOS, mPM.hmacKey);
            osPlain = attachStreamEncryptor(header, hbos);
        }
        OutputStream osXml;
        try {
            if (mPM.compressionAlgorithm == PwCompressionAlgorithm.Gzip) {
                osXml = new GZIPOutputStream(osPlain);
            } else {
                osXml = osPlain;
            }
            if (header.version >= PwDbHeaderV4.FILE_VERSION_32_4) {
                PwDbInnerHeaderOutputV4 ihOut = new PwDbInnerHeaderOutputV4((PwDatabaseV4) mPM, header, osXml);
                ihOut.output();
            }
            outputDatabase(osXml);
            osXml.close();
        } catch (IllegalArgumentException e) {
            throw new PwDbOutputException(e);
        } catch (IllegalStateException e) {
            throw new PwDbOutputException(e);
        }
    } catch (IOException e) {
        throw new PwDbOutputException(e);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) PwDbOutputException(com.keepassdroid.database.exception.PwDbOutputException) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CipherOutputStream(javax.crypto.CipherOutputStream) LEDataOutputStream(com.keepassdroid.stream.LEDataOutputStream) HmacBlockOutputStream(com.keepassdroid.stream.HmacBlockOutputStream) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream) OutputStream(java.io.OutputStream) HmacBlockOutputStream(com.keepassdroid.stream.HmacBlockOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream)

Example 3 with HashedBlockOutputStream

use of com.keepassdroid.stream.HashedBlockOutputStream in project KeePassDX by Kunzisoft.

the class HashedBlock method testGZIPStream.

public void testGZIPStream() throws IOException {
    final int testLength = 32000;
    byte[] orig = new byte[testLength];
    rand.nextBytes(orig);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    HashedBlockOutputStream hos = new HashedBlockOutputStream(bos);
    GZIPOutputStream zos = new GZIPOutputStream(hos);
    zos.write(orig);
    zos.close();
    byte[] compressed = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    HashedBlockInputStream his = new HashedBlockInputStream(bis);
    GZIPInputStream zis = new GZIPInputStream(his);
    byte[] uncompressed = new byte[testLength];
    int read = 0;
    while (read != -1 && testLength - read > 0) {
        read += zis.read(uncompressed, read, testLength - read);
    }
    assertArrayEquals("Output not equal to input", orig, uncompressed);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashedBlockOutputStream(com.keepassdroid.stream.HashedBlockOutputStream) HashedBlockInputStream(com.keepassdroid.stream.HashedBlockInputStream)

Aggregations

HashedBlockOutputStream (com.keepassdroid.stream.HashedBlockOutputStream)3 HashedBlockInputStream (com.keepassdroid.stream.HashedBlockInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 PwDbOutputException (com.keepassdroid.database.exception.PwDbOutputException)1 HmacBlockOutputStream (com.keepassdroid.stream.HmacBlockOutputStream)1 LEDataOutputStream (com.keepassdroid.stream.LEDataOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 CipherOutputStream (javax.crypto.CipherOutputStream)1