use of ch.cyberduck.core.cryptomator.random.RandomNonceGenerator in project cyberduck by iterate-ch.
the class BoxWriteFeatureTest method testWriteVaultWithTimeStamp.
@Test
public void testWriteVaultWithTimeStamp() throws Exception {
final BoxFileidProvider fileid = new BoxFileidProvider(session);
final Path container = new BoxDirectoryFeature(session, fileid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final Path vault = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final CryptoVault cryptomator = new CryptoVault(vault);
cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final CryptoWriteFeature feature = new CryptoWriteFeature<>(session, new BoxWriteFeature(session, fileid), cryptomator);
final byte[] content = RandomUtils.nextBytes(6 * 1024 * 1024);
final TransferStatus writeStatus = new TransferStatus();
final FileHeader header = cryptomator.getFileHeaderCryptor().create();
writeStatus.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
writeStatus.setNonces(new RandomNonceGenerator());
writeStatus.setLength(-1L);
writeStatus.setTimestamp(Instant.now().getEpochSecond());
final StatusOutputStream out = feature.write(test, writeStatus, new DisabledConnectionCallback());
final ByteArrayInputStream in = new ByteArrayInputStream(content);
final TransferStatus progress = new TransferStatus();
final BytecountStreamListener count = new BytecountStreamListener();
new StreamCopier(new TransferStatus(), progress).withListener(count).transfer(in, out);
assertEquals(content.length, count.getSent());
assertEquals(content.length, count.getRecv());
assertNotNull(out.getStatus());
assertTrue(new CryptoFindFeature(session, new BoxFindFeature(session, fileid), cryptomator).find(test));
final byte[] compare = new byte[content.length];
final InputStream stream = new CryptoReadFeature(session, new BoxReadFeature(session, fileid), cryptomator).read(test, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
IOUtils.readFully(stream, compare);
stream.close();
assertArrayEquals(content, compare);
cryptomator.getFeature(session, Delete.class, new BoxDeleteFeature(session, fileid)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.cryptomator.random.RandomNonceGenerator in project cyberduck by iterate-ch.
the class BufferWriteFeatureTest method testWriteVault.
@Test
public void testWriteVault() throws Exception {
final BoxFileidProvider fileid = new BoxFileidProvider(session);
final Path container = new BoxDirectoryFeature(session, fileid).mkdir(new Path(new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), new TransferStatus());
final Path vault = new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final CryptoVault cryptomator = new CryptoVault(vault);
cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final CryptoWriteFeature feature = new CryptoWriteFeature<>(session, new BufferWriteFeature(session), cryptomator);
final byte[] content = RandomUtils.nextBytes(1024 * 1024);
final TransferStatus writeStatus = new TransferStatus();
final FileHeader header = cryptomator.getFileHeaderCryptor().create();
writeStatus.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
writeStatus.setNonces(new RandomNonceGenerator());
writeStatus.setChecksum(feature.checksum(test, new TransferStatus()).compute(new ByteArrayInputStream(content), new TransferStatus()));
writeStatus.setLength(content.length);
final StatusOutputStream out = feature.write(test, writeStatus, new DisabledConnectionCallback());
final ByteArrayInputStream in = new ByteArrayInputStream(content);
final TransferStatus progress = new TransferStatus();
final BytecountStreamListener count = new BytecountStreamListener();
new StreamCopier(new TransferStatus(), progress).withListener(count).transfer(in, out);
assertEquals(content.length, count.getSent());
assertEquals(content.length, count.getRecv());
assertTrue(new CryptoFindFeature(session, new BoxFindFeature(session, fileid), cryptomator).find(test));
final byte[] compare = new byte[content.length];
final InputStream stream = new CryptoReadFeature(session, new BoxReadFeature(session, fileid), cryptomator).read(test, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
IOUtils.readFully(stream, compare);
stream.close();
assertArrayEquals(content, compare);
cryptomator.getFeature(session, Delete.class, new BoxDeleteFeature(session, fileid)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of ch.cyberduck.core.cryptomator.random.RandomNonceGenerator in project cyberduck by iterate-ch.
the class CryptoDirectoryV7Feature method mkdir.
@Override
public Path mkdir(final Path folder, final TransferStatus status) throws BackgroundException {
final Path encrypt = vault.encrypt(session, folder, random.random(), false);
final String directoryId = encrypt.attributes().getDirectoryId();
;
// Create metadata file for directory
final Path directoryMetadataFolder = vault.encrypt(session, folder, true);
final Path directoryMetadataFile = new Path(session._getFeature(Directory.class).mkdir(directoryMetadataFolder, new TransferStatus().withRegion(status.getRegion())), CryptoDirectoryV7Provider.DIRECTORY_METADATAFILE, EnumSet.of(Path.Type.file));
if (log.isDebugEnabled()) {
log.debug(String.format("Write metadata %s for folder %s", directoryMetadataFile, folder));
}
new ContentWriter(session).write(directoryMetadataFile, directoryId.getBytes(StandardCharsets.UTF_8));
final Path intermediate = encrypt.getParent();
if (!find.find(intermediate)) {
delegate.mkdir(intermediate, new TransferStatus().withRegion(status.getRegion()));
}
// Write header
final FileHeader header = vault.getFileHeaderCryptor().create();
status.setHeader(vault.getFileHeaderCryptor().encryptHeader(header));
status.setNonces(new RandomNonceGenerator());
final Path target = delegate.withWriter(new CryptoWriteFeature<>(session, writer, vault)).mkdir(encrypt, status);
// Implementation may return new copy of attributes without encryption attributes
target.attributes().setDirectoryId(directoryId);
target.attributes().setDecrypted(folder);
// Make reference of encrypted path in attributes of decrypted file point to metadata file
final Path decrypt = vault.decrypt(session, vault.encrypt(session, target, true));
decrypt.attributes().setFileId(directoryMetadataFolder.attributes().getFileId());
decrypt.attributes().setVersionId(directoryMetadataFolder.attributes().getVersionId());
return decrypt;
}
use of ch.cyberduck.core.cryptomator.random.RandomNonceGenerator in project cyberduck by iterate-ch.
the class DriveWriteFeatureTest method testWriteWithCache.
@Test
public void testWriteWithCache() throws Exception {
final TransferStatus status = new TransferStatus();
final int length = 1048576;
final byte[] content = RandomUtils.nextBytes(length);
status.setLength(content.length);
final Path home = DriveHomeFinderService.MYDRIVE_FOLDER;
final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final CryptoVault cryptomator = new CryptoVault(vault);
cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final DriveFileIdProvider fileid = new DriveFileIdProvider(session);
final CryptoWriteFeature<File> writer = new CryptoWriteFeature<>(session, new DriveWriteFeature(session, fileid), cryptomator);
final FileHeader header = cryptomator.getFileHeaderCryptor().create();
status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
status.setNonces(new RandomNonceGenerator());
status.setChecksum(writer.checksum(test, status).compute(new ByteArrayInputStream(content), status));
final OutputStream out = writer.write(test, status, new DisabledConnectionCallback());
assertNotNull(out);
new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
out.close();
assertTrue(new CryptoFindFeature(session, new DriveFindFeature(session, fileid), cryptomator).find(test));
final Path found = new CryptoListService(session, new DriveListService(session, fileid), cryptomator).list(test.getParent(), new DisabledListProgressListener()).find(new SimplePathPredicate(test));
final String fileId = found.attributes().getFileId();
assertNotNull(fileId);
final Cache<Path> cache = new PathCache(1);
final AttributedList<Path> list = new AttributedList<>();
list.add(found);
cache.put(vault, list);
assertEquals(content.length, cache.get(vault).get(0).attributes().getSize());
assertEquals(content.length, found.attributes().getSize());
assertEquals(content.length, writer.append(test, status.withRemote(found.attributes())).size, 0L);
{
final PathAttributes attributes = new CryptoAttributesFeature(session, new DriveAttributesFinderFeature(session, fileid), cryptomator).find(test);
assertEquals(content.length, attributes.getSize());
assertEquals(fileId, found.attributes().getFileId());
}
{
final PathAttributes attributes = new CryptoAttributesFeature(session, new DefaultAttributesFinderFeature(session), cryptomator).find(test);
assertEquals(content.length, attributes.getSize());
assertEquals(fileId, found.attributes().getFileId());
}
{
final PathAttributes attributes = new CryptoAttributesFeature(session, new DefaultAttributesFinderFeature(session), cryptomator).find(test);
assertEquals(content.length, attributes.getSize());
assertEquals(fileId, found.attributes().getFileId());
}
assertEquals(content.length, cache.get(vault).get(0).attributes().getSize());
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
final InputStream in = new CryptoReadFeature(session, new DriveReadFeature(session, fileid), cryptomator).read(test, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
new StreamCopier(status, status).transfer(in, buffer);
assertArrayEquals(content, buffer.toByteArray());
cryptomator.getFeature(session, Delete.class, new DriveDeleteFeature(session, fileid)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
session.close();
}
use of ch.cyberduck.core.cryptomator.random.RandomNonceGenerator in project cyberduck by iterate-ch.
the class SFTPReadFeatureTest method testReadRange.
@Test
public void testReadRange() throws Exception {
final TransferStatus status = new TransferStatus();
final int length = 140000;
final byte[] content = RandomUtils.nextBytes(length);
status.setLength(content.length);
final Path home = new SFTPHomeDirectoryService(session).find();
final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final CryptoVault cryptomator = new CryptoVault(vault);
cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final CryptoWriteFeature<Void> writer = new CryptoWriteFeature<>(session, new SFTPWriteFeature(session), cryptomator);
final FileHeader header = cryptomator.getFileHeaderCryptor().create();
status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
status.setNonces(new RandomNonceGenerator());
status.setChecksum(writer.checksum(test, status).compute(new ByteArrayInputStream(content), status));
final OutputStream out = writer.write(test, status, new DisabledConnectionCallback());
assertNotNull(out);
new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
out.close();
assertTrue(new CryptoFindFeature(session, new SFTPFindFeature(session), cryptomator).find(test));
final PathAttributes attributes = new CryptoListService(session, new SFTPListService(session), cryptomator).list(test.getParent(), new DisabledListProgressListener()).get(test).attributes();
assertEquals(content.length, attributes.getSize());
assertEquals(content.length, writer.append(test, status.withRemote(attributes)).size, 0L);
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(30000);
final TransferStatus read = new TransferStatus();
read.setOffset(40000);
read.setAppend(true);
// ensure to read at least two chunks
read.withLength(30000);
final InputStream in = new CryptoReadFeature(session, new SFTPReadFeature(session), cryptomator).read(test, read, new DisabledConnectionCallback());
new StreamCopier(read, read).withLimit(30000L).transfer(in, buffer);
final byte[] reference = new byte[30000];
System.arraycopy(content, 40000, reference, 0, reference.length);
assertArrayEquals(reference, buffer.toByteArray());
cryptomator.getFeature(session, Delete.class, new SFTPDeleteFeature(session)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Aggregations