Struct ffsend_api::reader::EncryptedFileWriter
source · pub struct EncryptedFileWriter { /* private fields */ }
Expand description
A lazy file writer, that decrypt the file with the given cipher
and verifies it with the tag appended to the end of the input data.
This writer is lazy because the input data is decrypted and written to the specified file on the fly, instead of buffering all the data first. This greatly reduces memory usage for large files.
The length of the input data (including the appended tag) must be given when this reader is initialized. When all data including the tag is read, the decrypted data is verified with the tag. If the tag doesn’t match the decrypted data, a write error is returned on the last write. This writer will never write more bytes than the length initially specified.
This reader encrypts the input data with the given key and input vector.
A failed writing implies that no data could be written, or that the data wasn’t successfully decrypted because of an decryption or tag matching error. Such a fail means that the file will be incomplete or corrupted, and should therefore be removed from the disk.
It is highly recommended to invoke the verified()
method after writing
the file, to ensure the written file is indeed complete and fully verified.
Implementations
sourceimpl EncryptedFileWriter
impl EncryptedFileWriter
sourcepub fn new(
file: File,
len: usize,
cipher: Cipher,
key: &[u8],
iv: &[u8]
) -> Result<Self, Error>
pub fn new(
file: File,
len: usize,
cipher: Cipher,
key: &[u8],
iv: &[u8]
) -> Result<Self, Error>
Construct a new encrypted file writer.
The file to write to must be given to file
, which must be open for
writing. The total length of the input data in bytes must be given to
len
, which includes both the file bytes and the appended tag.
For decryption, a cipher
, key
and iv
must also be given.
sourcepub fn verified(&self) -> bool
pub fn verified(&self) -> bool
Check whether the decrypted data is succesfsully verified.
If this method returns true the following is implied:
- The complete file has been written.
- The complete file was successfully decrypted.
- The included tag matches the decrypted file.
It is highly recommended to invoke this method and check the verification after writing the file using this writer.
Trait Implementations
sourceimpl Write for EncryptedFileWriter
impl Write for EncryptedFileWriter
The writer trait implementation.
sourcefn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
sourcefn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)