@@ -31,21 +31,43 @@ Advanced Usage: Message Digests
3131^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
3333.. warning ::
34- This feature is not supported in Python 3.14+.
34+ This feature has a different syntax in Python 3.14+.
3535
3636Actions can also be used to perform more complex operations, such as calculating
3737checksums or cryptographic hash values before or after processing fields. For
3838example, you can use an action to automatically wrap a sequence of fields with a
3939specialized message digest like SHA256 (see :class: `~caterpillar.py.Digest `):
4040
41- .. code-block :: python
41+ .. tab-set ::
42+
43+ .. tab-item :: Python <= 3.13
44+
45+ .. code-block :: python
46+
47+ @struct
48+ class Format :
49+ key: b " ..."
50+ with Sha2_256(" hash" , verify = True ):
51+ user_data: Bytes(50 )
52+ # the 'hash' attribute will be set automatically
53+
54+
55+ .. tab-item :: Python >= 3.14
56+
57+ .. code-block :: python
58+
59+ @struct
60+ class Format :
61+ key: b " ..."
62+ _hash_begin: DigestField.begin(" hash" , Sha2_256_Algo)
63+ user_data: Bytes(50 )
64+ # the 'hash' attribute must be set manually
65+ hash : DigestField(" hash" , Bytes(32 ), verify = True ) = None
66+ # or
67+ hash : Sha2_256_Field(" hash" , verify = True ) = None
68+
69+
4270
43- @struct
44- class Format :
45- a: uint8
46- with Sha2_256(" hash" , verify = True ):
47- user_data: Bytes(50 )
48- # the 'hash' attribute will be set automatically
4971
5072 In this example, the :code: `user_data ` field is wrapped with the :code: `Sha2_256 ` digest action.
5173When the struct is packed, a SHA256 hash is computed for the :code: `user_data ` and stored
@@ -54,17 +76,36 @@ with, the verification step will raise an error.
5476
5577The resulting struct includes the following fields:
5678
57- .. code-block :: python
5879
59- >> > Format.__struct__.fields
60- [
61- Field(' key' , struct = < ConstBytes> , ... ),
62- (Action(Digest.begin), None ),
63- Field(' user_data' , struct = < Bytes> , ... ),
64- (Action(Digest.end_pack, Digest.end_unpack), None ),
65- Field(' hash' , struct = < Bytes> , ... ),
66- (UnpackAction(Digest.verfiy), None )
67- ]
80+ .. tab-set ::
81+
82+ .. tab-item :: Python <= 3.13
83+
84+ .. code-block :: python
85+
86+ >> > Format.__struct__.fields
87+ [
88+ Field(' key' , struct = < ConstBytes> , ... ),
89+ (Action(Digest.begin), None ),
90+ Field(' user_data' , struct = < Bytes> , ... ),
91+ (Action(Digest.end_pack, Digest.end_unpack), None ),
92+ Field(' hash' , struct = < Bytes> , ... ),
93+ (UnpackAction(Digest.verfiy), None )
94+ ]
95+
96+
97+ .. tab-item :: Python >= 3.14
98+
99+ .. code-block :: python
100+
101+ >> > Format.__struct__.fields
102+ [
103+ Field(' key' , struct = < ConstBytes> , ... ),
104+ (< DigestFieldAction> , None ),
105+ Field(' user_data' , struct = < Bytes> , ... ),
106+ < DigestField> ,
107+ ]
108+
68109
69110 Here, you can see that:
70111
0 commit comments