-
Notifications
You must be signed in to change notification settings - Fork 96
fix(zipcrypto): Support streaming ZipCrypto encryption, don't store entire file in memory #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @roblabla, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a significant memory consumption issue in ZipCrypto encryption by enabling true streaming capabilities. Previously, the entire file had to be loaded into memory to calculate the CRC for password verification. By enforcing the use of data descriptors for ZipCrypto, the system can now use the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly implements streaming encryption for ZipCrypto by forcing the use of a data descriptor, which avoids buffering the entire file in memory. The changes to use the last modified time for password verification are well-implemented and clearly explained. The test suite has also been updated to cover this new streaming behavior. I have one suggestion to improve the performance of the new streaming writer.
| let mut buf = buf.to_vec(); | ||
| for byte in buf.iter_mut() { | ||
| *byte = self.keys.encrypt_byte(*byte); | ||
| } | ||
| self.writer.write_all(&buf)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of write allocates a new Vec on every call via buf.to_vec(). This can be inefficient, especially when write is called frequently (e.g., by io::copy), as it causes a heap allocation for each call.
To improve performance, you could use a stack-allocated buffer and process the input in chunks. This avoids repeated heap allocations.
| let mut buf = buf.to_vec(); | |
| for byte in buf.iter_mut() { | |
| *byte = self.keys.encrypt_byte(*byte); | |
| } | |
| self.writer.write_all(&buf)?; | |
| const CHUNK_SIZE: usize = 4096; | |
| let mut temp_buf = [0u8; CHUNK_SIZE]; | |
| for chunk in buf.chunks(CHUNK_SIZE) { | |
| let encrypted_chunk = &mut temp_buf[..chunk.len()]; | |
| for (i, &byte) in chunk.iter().enumerate() { | |
| encrypted_chunk[i] = self.keys.encrypt_byte(byte); | |
| } | |
| self.writer.write_all(encrypted_chunk)?; | |
| } |
The current implementation of ZipCrypto reads the entire file in memory before encrypting it. This is because ZipCrypto expects the last two bytes of the header to contain the CRC of the file when not using a data descriptor, to be able to quickly check if the password is correct. This is slightly problematic for my use-cases, as I need to encrypt a (potentially very large) file using ZipCrypto, so reading the whole thing in memory would cause issues.
There is a different way to solve this problem however. When using a data descriptor, ZipCrypto will use the last modified time instead as the password verification bytes. This is something we can have before we start writing the file, and so allows us to do the streaming without issues.
This PR forces ZipCrypto files to go through a Data Descriptor so we don't have to do the whole "read the whole file in memory", and also get streaming support working.