use of com.dracoon.sdk.crypto.model.PlainFileKey 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