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);
}
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);
}
}
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);
}
Aggregations