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

1"""This module implements thin layer on top of data blocks read from 

2the container. 

3 

4""" 

5 

6from typing import Optional 

7 

8 

9class DataBlock: 

10 """This class represents single data block - either successfully 

11 decrypted or opaque. 

12 

13 """ 

14 

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. 

23 

24 Parameters: 

25 enc: encrypted data of the packet including nonce and MAC 

26 clear: decrypted packet data - if available 

27 

28 """ 

29 self._ciphertext = enc 

30 self._cleartext = clear 

31 self._dek_index = idx 

32 self._offset = off 

33 

34 @property 

35 def ciphertext(self) -> bytes: 

36 """The encrypted data of the whole packet accessor. 

37 

38 Returns: 

39 The ecrypted packet as-is. 

40 

41 """ 

42 return self._ciphertext 

43 

44 @property 

45 def cleartext(self) -> Optional[bytes]: 

46 """The decrypted data of the packet accessor. 

47 

48 Returns: 

49 The cleartext of the packet contents if available, None otherwise. 

50 

51 """ 

52 return self._cleartext 

53 

54 @property 

55 def is_deciphered(self) -> bool: 

56 """Predicate to test whether the cleartext contents of this 

57 packet can be read. 

58 

59 """ 

60 return self._cleartext is not None 

61 

62 @property 

63 def dek_index(self): 

64 """Returns the DEK index (to avoid leaking the actual key)""" 

65 return self._dek_index 

66 

67 @property 

68 def offset(self): 

69 """Returns the offset this block starts at (in original 

70 cleartext data)""" 

71 return self._offset 

72 

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