From f8cc1091a6db659388e336503009acd6ec9e0d01 Mon Sep 17 00:00:00 2001 From: MatrixEditor <58256046+MatrixEditor@users.noreply.github.com> Date: Mon, 30 Dec 2024 19:01:42 +0100 Subject: [PATCH] Updated README to include Digests --- + remove unnecessary mode checks in digest verification --- README.md | 17 +++++++++++------ src/caterpillar/fields/__init__.py | 20 ++++++++++++++++++-- src/caterpillar/fields/digest.py | 8 -------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8d45d45..fc02b61 100644 --- a/README.md +++ b/README.md @@ -40,15 +40,20 @@ class Format: b: int32 length: uint8 # String fields with computed lengths name: String(this.length) # -> you can also use Prefixed(uint8) - names: CString[uint8::] # Sequences with prefixed, computed lengths + with Md5(name="hash", verify=True): # wraps all following fields and creates a new attr + names: CString[uint8::] # Sequences with prefixed, computed lengths + # Instantiation (keyword-only arguments, magic is auto-inferred): obj = Format(a=1, b=2, length=3, name="foo", names=["a", "b"]) -# Packing the object: -blob = pack(obj) # objects of struct classes can be packed right away -# results in: b'ITS MAGIC\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00' -# Unpacking the binary data: -obj2 = unpack(blob, Format) +# Packing the object, reads as 'PACK obj FROM Format' +# objects of struct classes can be packed right away +blob = pack(obj, Format) +# results in: b'ITS MAGIC\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00\xf55... + +# Unpacking the binary data, reads as 'UNPACK Format FROM blob' +obj2 = unpack(Format, blob) +assert obj2.hash == obj.hash ``` This library offers extensive functionality beyond basic struct handling. For further details diff --git a/src/caterpillar/fields/__init__.py b/src/caterpillar/fields/__init__.py index c7d7dd4..2682262 100644 --- a/src/caterpillar/fields/__init__.py +++ b/src/caterpillar/fields/__init__.py @@ -53,7 +53,7 @@ Uuid, Aligned, align, - Lazy + Lazy, ) from .varint import VarInt, VARINT_LSB, vint from .compression import ( @@ -68,4 +68,20 @@ from .pointer import uintptr, intptr, offintptr, offuintptr, Pointer from .conditional import ConditionalChain, If, Else, ElseIf from .hook import IOHook -from .digest import Digest, Algorithm +from .digest import ( + Digest, + Algorithm, + Md5, + Sha1, + Sha2_256, + Sha2_224, + Sha2_384, + Sha2_512, + Sha3_224, + Sha3_256, + Sha3_384, + Sha3_512, + Crc32, + Adler, + HMAC +) diff --git a/src/caterpillar/fields/digest.py b/src/caterpillar/fields/digest.py index 55c6787..a030397 100644 --- a/src/caterpillar/fields/digest.py +++ b/src/caterpillar/fields/digest.py @@ -384,10 +384,6 @@ def verfiy(self, context: _ContextLike) -> None: :raises ValidationError: If the digest verification fails. """ # we assume self._verify is True - if context._root.mode == MODE_PACK: - # we don'T verify in packing mode as we created that digest - return - digest = context.__context_getattr__(self.path or self.name) if digest != self._digest: digest_raw = digest.hex() if isinstance(digest, bytes) else digest @@ -491,11 +487,7 @@ def _cryptography_hash_algo(cls: Type[hashes.HashAlgorithm]): Sha3_256_Algo = _cryptography_hash_algo(hashes.SHA3_256) Sha3_384_Algo = _cryptography_hash_algo(hashes.SHA3_384) Sha3_512_Algo = _cryptography_hash_algo(hashes.SHA3_512) - Md5_Algo = _cryptography_hash_algo(hashes.MD5) - Blake2b_Algo = _cryptography_hash_algo(hashes.BLAKE2b) - Blake2s_Algo = _cryptography_hash_algo(hashes.BLAKE2s) - Sm3_Algo = _cryptography_hash_algo(hashes.SM3) Sha1 = _hash_digest(Sha1_Algo, Bytes(20)) Sha2_224 = _hash_digest(Sha2_224_Algo, Bytes(28))