use of ch.cyberduck.core.sds.SDSApiClient in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStreamTest method testEncryptDecryptZeroBytes.
@Test
public void testEncryptDecryptZeroBytes() throws Exception {
final byte[] content = RandomUtils.nextBytes(0);
final ByteArrayInputStream plain = new ByteArrayInputStream(content);
final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
public SDSApiClient getClient() {
return new SDSApiClient(new MockHttpClient());
}
};
final TransferStatus status = new TransferStatus();
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
IOUtils.copy(encryptInputStream, os, 42);
encryptInputStream.close();
out.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
final byte[] compare = new byte[content.length];
IOUtils.read(cryptInputStream, compare);
assertArrayEquals(content, compare);
}
use of ch.cyberduck.core.sds.SDSApiClient in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStreamTest method testEncryptDecryptWithContentSizeMultipleOfEncryptingBufferSize.
@Test
public void testEncryptDecryptWithContentSizeMultipleOfEncryptingBufferSize() throws Exception {
final byte[] content = RandomUtils.nextBytes(1024 * 1024);
final ByteArrayInputStream plain = new ByteArrayInputStream(content);
final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
public SDSApiClient getClient() {
return new SDSApiClient(new MockHttpClient());
}
};
final TransferStatus status = new TransferStatus();
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
new StreamCopier(StreamCancelation.noop, StreamProgress.noop).withLimit((long) content.length).withChunksize(32768).transfer(encryptInputStream, os);
encryptInputStream.close();
out.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
assertNotNull(fileKey.getTag());
final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
final byte[] compare = new byte[content.length];
IOUtils.read(cryptInputStream, compare);
assertArrayEquals(content, compare);
}
use of ch.cyberduck.core.sds.SDSApiClient in project cyberduck by iterate-ch.
the class TripleCryptEncryptingInputStreamTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
final byte[] content = RandomUtils.nextBytes(1024 * 1024 + 1);
final ByteArrayInputStream plain = new ByteArrayInputStream(content);
final PlainFileKey key = Crypto.generateFileKey(PlainFileKey.Version.AES256GCM);
final SDSSession session = new SDSSession(new Host(new TestProtocol()), new DisabledX509TrustManager(), new DefaultX509KeyManager()) {
@Override
public SDSApiClient getClient() {
return new SDSApiClient(new MockHttpClient());
}
};
final TransferStatus status = new TransferStatus();
final ObjectWriter writer = session.getClient().getJSON().getContext(null).writerFor(FileKey.class);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.writeValue(out, TripleCryptConverter.toSwaggerFileKey(key));
status.setFilekey(ByteBuffer.wrap(out.toByteArray()));
final TripleCryptEncryptingInputStream encryptInputStream = new TripleCryptEncryptingInputStream(session, plain, Crypto.createFileEncryptionCipher(key), status);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
new StreamCopier(StreamCancelation.noop, StreamProgress.noop).withLimit((long) content.length).withChunksize(32768).transfer(encryptInputStream, os);
encryptInputStream.close();
out.close();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
final ObjectReader reader = session.getClient().getJSON().getContext(null).readerFor(FileKey.class);
final FileKey fileKey = reader.readValue(status.getFilekey().array());
assertNotNull(fileKey.getTag());
final TripleCryptDecryptingInputStream cryptInputStream = new TripleCryptDecryptingInputStream(is, Crypto.createFileDecryptionCipher(TripleCryptConverter.toCryptoPlainFileKey(fileKey)), CryptoUtils.stringToByteArray(fileKey.getTag()));
final byte[] compare = new byte[content.length];
IOUtils.read(cryptInputStream, compare);
assertArrayEquals(content, compare);
}
Aggregations