Browse Source

- decrypt whole file, don't stop if enough data has been read from underlying fs
- write "length = 0" into file header until everything is encrypted

(tested on windows, everything is fine here)

Sebastian Stenzel 9 năm trước cách đây
mục cha
commit
c03bdd8425

+ 1 - 7
main/filesystem-crypto/src/main/java/org/cryptomator/crypto/engine/impl/FileContentDecryptorImpl.java

@@ -32,7 +32,6 @@ import javax.crypto.SecretKey;
 import javax.crypto.ShortBufferException;
 import javax.crypto.spec.IvParameterSpec;
 
-import org.apache.commons.codec.binary.Hex;
 import org.cryptomator.crypto.engine.AuthenticationFailedException;
 import org.cryptomator.crypto.engine.FileContentCryptor;
 import org.cryptomator.crypto.engine.FileContentDecryptor;
@@ -49,7 +48,6 @@ class FileContentDecryptorImpl implements FileContentDecryptor {
 	private final Supplier<Mac> hmacSha256;
 	private final FileHeader header;
 	private final boolean authenticate;
-	private final LongAdder ciphertextBytesScheduledForDecryption = new LongAdder();
 	private final LongAdder cleartextBytesDecrypted = new LongAdder();
 	private ByteBuffer ciphertextBuffer = ByteBuffer.allocate(CHUNK_SIZE);
 	private long chunkNumber = 0;
@@ -68,14 +66,10 @@ class FileContentDecryptorImpl implements FileContentDecryptor {
 
 	@Override
 	public void append(ByteBuffer ciphertext) throws InterruptedException {
-		long numChunksNeeded = (contentLength() - 1) / PAYLOAD_SIZE + 1;
-		long numCiphertextBytesNeeded = numChunksNeeded * CHUNK_SIZE;
-
-		if (ciphertext == FileContentCryptor.EOF || ciphertextBytesScheduledForDecryption.sum() >= numCiphertextBytesNeeded) {
+		if (ciphertext == FileContentCryptor.EOF) {
 			submitCiphertextBuffer();
 			submitEof();
 		} else {
-			ciphertextBytesScheduledForDecryption.add(ciphertext.remaining());
 			while (ciphertext.hasRemaining()) {
 				ByteBuffers.copy(ciphertext, ciphertextBuffer);
 				submitCiphertextBufferIfFull();

+ 1 - 1
main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/CryptoWritableFile.java

@@ -40,7 +40,7 @@ class CryptoWritableFile implements WritableFile {
 
 	private void initialize(long firstCleartextByte) {
 		encryptor = cryptor.createFileContentEncryptor(Optional.empty(), firstCleartextByte);
-		file.position(cryptor.getHeaderSize()); // skip header size, header is written on close
+		writeHeader(); // write header with "zero content length" to avoid read access while still writing
 		writeTask = executorService.submit(new CiphertextWriter(file, encryptor));
 	}