Skip to content

Commit

Permalink
Fix issue with lower case base32hex decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Mar 17, 2024
1 parent 7447f86 commit ca0e2a1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [1.0.9] - Unreleased
- Updated dependencies
- Fixed issue with decoding lower case base32hex strings
- Added support for MSCDI validation

## [1.0.8] - 2024-01-30
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [1.0.9] - Unreleased
- Updated dependencies
- Fixed issue with decoding lower case base32hex strings
- Added support for MSCDI validation

## [1.0.8] - 2024-01-30
Expand Down
3 changes: 2 additions & 1 deletion iscc_core/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ def decode_base32hex(code):
see: https://tools.ietf.org/html/rfc4648#page-10
"""
b32 = code.translate(hex_to_b32)
# Make sure we use upper-case version for translation
b32 = code.upper().translate(hex_to_b32)
return decode_base32(b32)


Expand Down
4 changes: 2 additions & 2 deletions iscc_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def rnd(cls, mt=None, st=None, bits=64, data=None):
return cls((mt, st, vs, ln_code, data))

@property
def mc_bytes(self):
def mc_bytes(self) -> bytes:
"""ISCC header + body with multicodec prefix."""
return MC_PREFIX + self.bytes

Expand All @@ -276,7 +276,7 @@ def mf_base32(self) -> str:

@property
def mf_base32hex(self) -> str:
"""Multiformats base32 encoded."""
"""Multiformats base32hex encoded."""
return "v" + encode_base32hex(self.mc_bytes).lower()

@property
Expand Down
7 changes: 7 additions & 0 deletions tests/test_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ def test_encode_base32hex():
assert ic.encode_base32hex(b"hello world") == "D1IMOR3F41RMUSJCCG"


def test_base32hex_roundtrip_lower():
data = bytes.fromhex("cc01cd9d2b7d247a8333f7b0b7d2cda8056c3d15eef738c1962e9148624feac1c14f")
expected_b32hex = "PG0SR79BFKI7L0PJUUOBFKMDL02MOF8LTRRJHGCM5Q8KGOIFTB0S2JO"
assert ic.encode_base32hex(data) == "PG0SR79BFKI7L0PJUUOBFKMDL02MOF8LTRRJHGCM5Q8KGOIFTB0S2JO"
assert ic.decode_base32hex(expected_b32hex.lower()) == data


def test_decode_base32hex():
assert ic.decode_base32hex("D1IMOR3F41RMUSJCCG") == b"hello world"

Expand Down

0 comments on commit ca0e2a1

Please sign in to comment.