-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from MatrixEditor/fix/prefixed-encoding
[FIX] Prefixed `encoding` Parameter
- Loading branch information
Showing
8 changed files
with
71 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from rich import print | ||
from cryptography.hazmat.primitives import hashes | ||
|
||
from caterpillar.py import struct, Bytes, pack, unpack, this | ||
from caterpillar.fields.digest import HMAC | ||
|
||
|
||
@struct | ||
class Format: | ||
key: b"MAGIC" | ||
|
||
with HMAC(this.key, hashes.SHA256(), "hmac", verify=True): | ||
user_data: Bytes(11) | ||
|
||
# attribute 'hmac' is added by HMAC | ||
|
||
|
||
print(Format(user_data=b"hello world")) | ||
print(Format.__struct__.fields) | ||
data = pack(Format(user_data=b"hello world")) | ||
print(data) | ||
print("unpacking..") | ||
print(unpack(Format, data[:-1] + b"\x00")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,5 +83,5 @@ | |
Sha3_512, | ||
Crc32, | ||
Adler, | ||
HMAC | ||
HMAC, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from caterpillar.py import Prefixed, pack, unpack, uint8, Bytes, uint16 | ||
|
||
|
||
def test_prefixed_bytes(): | ||
atom = Prefixed(uint8) | ||
|
||
assert pack(b"foo", atom, as_field=True) == b"\x03foo" | ||
assert unpack(atom, b"\x03foo", as_field=True) == b"foo" | ||
|
||
|
||
def test_prefixed_encoding(): | ||
atom = Prefixed(uint8, encoding="utf-8") | ||
|
||
assert unpack(atom, b"\x03foo", as_field=True) == "foo" | ||
assert pack("foo", atom, as_field=True) == b"\x03foo" | ||
|
||
|
||
def test_prefixed_bytes_struct(): | ||
atom = Prefixed(uint8, Bytes(...)) | ||
|
||
assert unpack(atom, b"\x03foo", as_field=True) == b"foo" | ||
assert pack(b"foo", atom, as_field=True) == b"\x03foo" | ||
|
||
|
||
def test_prefixed_custom_struct(): | ||
atom = Prefixed(uint8, uint16[...]) | ||
|
||
assert unpack(atom, b"\x04\x01\x00\x01\x00", as_field=True) == [1, 1] | ||
assert pack([1, 1], atom, as_field=True) == b"\x04\x01\x00\x01\x00" |