use of com.dracoon.sdk.crypto.model.EncryptedDataContainer in project cyberduck by iterate-ch.
the class TripleCryptDecryptingInputStream method readNextChunk.
private int readNextChunk() throws IOException {
final ByteBuffer ciphertextBuf = ByteBuffer.allocate(SDSSession.DEFAULT_CHUNKSIZE);
final int read = IOUtils.read(proxy, ciphertextBuf.array());
if (lastread == 0) {
return IOUtils.EOF;
}
ciphertextBuf.position(read);
ciphertextBuf.flip();
try {
final PlainDataContainer pDataContainer;
if (read == 0) {
final PlainDataContainer c1 = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
final PlainDataContainer c2 = cipher.doFinal(new EncryptedDataContainer(null, tag));
pDataContainer = new PlainDataContainer(ArrayUtils.addAll(c1.getContent(), c2.getContent()));
} else {
pDataContainer = cipher.processBytes(createEncryptedDataContainer(ciphertextBuf.array(), read, null));
}
final byte[] content = pDataContainer.getContent();
buffer = ByteBuffer.allocate(content.length);
buffer.put(content);
buffer.flip();
lastread = read;
return content.length;
} catch (CryptoException e) {
throw new IOException(e);
}
}
use of com.dracoon.sdk.crypto.model.EncryptedDataContainer in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStream method fillBuffer.
private void fillBuffer(final int len) throws IOException, CryptoException {
// Read ahead to the next chunk end
final int allocate = (len / IOUtils.DEFAULT_BUFFER_SIZE) * IOUtils.DEFAULT_BUFFER_SIZE + IOUtils.DEFAULT_BUFFER_SIZE;
buffer = ByteBuffer.allocate(allocate);
final byte[] plain = new byte[allocate];
final int read = IOUtils.read(proxy, plain, 0, allocate);
if (read > 0) {
int position = 0;
for (int chunkOffset = 0; chunkOffset < read; chunkOffset += SDSSession.DEFAULT_CHUNKSIZE) {
int chunkLen = Math.min(SDSSession.DEFAULT_CHUNKSIZE, read - chunkOffset);
final byte[] bytes = Arrays.copyOfRange(plain, chunkOffset, chunkOffset + chunkLen);
final PlainDataContainer data = TripleCryptKeyPair.createPlainDataContainer(bytes, bytes.length);
final EncryptedDataContainer encrypted = cipher.processBytes(data);
final byte[] encBuf = encrypted.getContent();
System.arraycopy(encBuf, 0, buffer.array(), position, encBuf.length);
position += encBuf.length;
}
buffer.limit(position);
}
if (read < allocate) {
// EOF in proxy stream, finalize cipher and put remaining bytes into buffer
eof = true;
final EncryptedDataContainer encContainer = cipher.doFinal();
final byte[] content = encContainer.getContent();
buffer = this.combine(buffer, ByteBuffer.wrap(content));
final String tag = CryptoUtils.byteArrayToString(encContainer.getTag());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
if (null == fileKey.getTag()) {
// Only override if not already set pre-computed in bulk feature
fileKey.setTag(tag);
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, fileKey);
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
} else {
log.warn(String.format("Skip setting tag in file key already found in %s", status));
}
}
}
Aggregations