Search in sources :

Example 6 with PlainFileKey

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);
}
Also used : PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) FileKey(ch.cyberduck.core.sds.io.swagger.client.model.FileKey) DisabledX509TrustManager(ch.cyberduck.core.ssl.DisabledX509TrustManager) TestProtocol(ch.cyberduck.core.TestProtocol) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Host(ch.cyberduck.core.Host) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpClient(com.google.api.client.testing.http.apache.MockHttpClient) SDSApiClient(ch.cyberduck.core.sds.SDSApiClient) SDSSession(ch.cyberduck.core.sds.SDSSession) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) DefaultX509KeyManager(ch.cyberduck.core.ssl.DefaultX509KeyManager) PlainFileKey(com.dracoon.sdk.crypto.model.PlainFileKey) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test)

Aggregations

PlainFileKey (com.dracoon.sdk.crypto.model.PlainFileKey)6 FileKey (ch.cyberduck.core.sds.io.swagger.client.model.FileKey)5 Host (ch.cyberduck.core.Host)4 TestProtocol (ch.cyberduck.core.TestProtocol)3 SDSApiClient (ch.cyberduck.core.sds.SDSApiClient)3 SDSSession (ch.cyberduck.core.sds.SDSSession)3 DefaultX509KeyManager (ch.cyberduck.core.ssl.DefaultX509KeyManager)3 DisabledX509TrustManager (ch.cyberduck.core.ssl.DisabledX509TrustManager)3 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)3 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 MockHttpClient (com.google.api.client.testing.http.apache.MockHttpClient)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Test (org.junit.Test)3 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)2 StreamCopier (ch.cyberduck.core.io.StreamCopier)2 ApiException (ch.cyberduck.core.sds.io.swagger.client.ApiException)2 NodesApi (ch.cyberduck.core.sds.io.swagger.client.api.NodesApi)2 CryptoException (com.dracoon.sdk.crypto.error.CryptoException)2