pub struct AesGcmFileRead { /* private fields */ }Expand description
Transparent decryption of AGS1 stream-encrypted files.
Implements the FileRead trait, providing random-access reads over
encrypted data. Each read() call determines which encrypted blocks
overlap the requested plaintext range, reads and decrypts them, then
returns the requested plaintext bytes.
§Usage
// (ignored: requires async runtime and concrete FileRead/FileWrite impls)
let reader = AesGcmFileRead::new(
inner_reader, // Box<dyn FileRead> for the encrypted file
cipher, // Arc<AesGcmCipher> with the DEK
aad_prefix.to_vec(),
encrypted_file_length,
)?;
// Read plaintext bytes transparently
let plaintext = reader.read(0..1024).await?;Implementations§
Source§impl AesGcmFileRead
impl AesGcmFileRead
Sourcepub fn new(
inner: Box<dyn FileRead>,
cipher: Arc<AesGcmCipher>,
aad_prefix: Box<[u8]>,
encrypted_file_length: u64,
) -> Result<Self>
pub fn new( inner: Box<dyn FileRead>, cipher: Arc<AesGcmCipher>, aad_prefix: Box<[u8]>, encrypted_file_length: u64, ) -> Result<Self>
Creates a new AesGcmFileRead for decrypting an AGS1 stream.
Computes the plaintext size and block layout from the encrypted file length. No I/O is performed; header validation happens implicitly when blocks are decrypted (GCM authentication will fail on corrupt data).
§Arguments
inner- Reader for the underlying encrypted filecipher- AES-GCM cipher initialized with the file’s DEKaad_prefix- AAD prefix from the file’sStandardKeyMetadataencrypted_file_length- Total byte length of the encrypted file
Sourcepub fn plaintext_length(&self) -> u64
pub fn plaintext_length(&self) -> u64
Returns the plaintext stream size in bytes.
Sourcepub fn calculate_plaintext_length(encrypted_file_length: u64) -> Result<u64>
pub fn calculate_plaintext_length(encrypted_file_length: u64) -> Result<u64>
Calculates the plaintext length from an encrypted file’s total length.
This is a static calculation matching Java’s
AesGcmInputStream.calculatePlaintextLength().
Trait Implementations§
Source§impl FileRead for AesGcmFileRead
impl FileRead for AesGcmFileRead
Source§fn read<'life0, 'async_trait>(
&'life0 self,
range: Range<u64>,
) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn read<'life0, 'async_trait>(
&'life0 self,
range: Range<u64>,
) -> Pin<Box<dyn Future<Output = Result<Bytes>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Reads and decrypts a plaintext byte range from the encrypted AGS1 stream.
The caller specifies a range in plaintext coordinates (e.g. “bytes 0..1024 of the original file”). This method translates that into the encrypted file layout and performs the following steps:
-
Map to blocks — divides the plaintext range by
PLAIN_BLOCK_SIZEto find which encrypted blocks (first_block..=last_block) contain the requested data. -
Single I/O read — calculates the contiguous byte range in the encrypted file that covers all needed blocks (including the 8-byte AGS1 header offset, 12-byte nonces, and 16-byte GCM tags) and fetches them in one call to the inner
FileRead. -
Decrypt per block — iterates over each cipher block in the response, decrypts it with AES-GCM using the per-block AAD (
aad_prefix || block_index), and slices out only the plaintext bytes that overlap the requested range. -
Assemble result — concatenates the slices into a single
Bytesbuffer matching exactlyrange.end - range.startbytes.
Because each block is independently encrypted with its own nonce and AAD, arbitrary random-access reads are supported without decrypting the entire file. GCM authentication is verified per-block, so any tampering is detected at the granularity of individual blocks.
Auto Trait Implementations§
impl Freeze for AesGcmFileRead
impl !RefUnwindSafe for AesGcmFileRead
impl Send for AesGcmFileRead
impl Sync for AesGcmFileRead
impl Unpin for AesGcmFileRead
impl !UnwindSafe for AesGcmFileRead
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: AsAny + ?Sized,
impl<T> Downcast for Twhere
T: AsAny + ?Sized,
§fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
Any.§fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
Any.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more