Coverage for oarepo_c4gh/crypt4gh/common/data_block.py: 100%
25 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-07 12:05 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-07 12:05 +0000
1"""This module implements thin layer on top of data blocks read from
2the container.
4"""
6from typing import Optional
9class DataBlock:
10 """This class represents single data block - either successfully
11 decrypted or opaque.
13 """
15 def __init__(
16 self,
17 enc: bytes,
18 clear: Optional[bytes],
19 idx: Optional[int],
20 off: Optional[int],
21 ) -> None:
22 """Initializes all the data block instance properties.
24 Parameters:
25 enc: encrypted data of the packet including nonce and MAC
26 clear: decrypted packet data - if available
28 """
29 self._ciphertext = enc
30 self._cleartext = clear
31 self._dek_index = idx
32 self._offset = off
34 @property
35 def ciphertext(self) -> bytes:
36 """The encrypted data of the whole packet accessor.
38 Returns:
39 The ecrypted packet as-is.
41 """
42 return self._ciphertext
44 @property
45 def cleartext(self) -> Optional[bytes]:
46 """The decrypted data of the packet accessor.
48 Returns:
49 The cleartext of the packet contents if available, None otherwise.
51 """
52 return self._cleartext
54 @property
55 def is_deciphered(self) -> bool:
56 """Predicate to test whether the cleartext contents of this
57 packet can be read.
59 """
60 return self._cleartext is not None
62 @property
63 def dek_index(self):
64 """Returns the DEK index (to avoid leaking the actual key)"""
65 return self._dek_index
67 @property
68 def offset(self):
69 """Returns the offset this block starts at (in original
70 cleartext data)"""
71 return self._offset
73 @property
74 def size(self):
75 """Returns the size of cleartext data of this packet -
76 regardless of whether it was deciphered."""
77 return len(self._ciphertext) - 16