Skip to content

Commit

Permalink
Updated README to include Digests
Browse files Browse the repository at this point in the history
---

+ remove unnecessary mode checks in digest verification
  • Loading branch information
MatrixEditor committed Dec 30, 2024
1 parent db031b4 commit f8cc109
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/caterpillar/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Uuid,
Aligned,
align,
Lazy
Lazy,
)
from .varint import VarInt, VARINT_LSB, vint
from .compression import (
Expand All @@ -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
)
8 changes: 0 additions & 8 deletions src/caterpillar/fields/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f8cc109

Please sign in to comment.