Skip to content

Fix: Support all hash types in Prefix.sum() - #75

Merged
seetadev merged 4 commits into
ipld:masterfrom
sumanjeet0012:fix-64-prefix-sum-hash-types
Jul 13, 2026
Merged

Fix: Support all hash types in Prefix.sum()#75
seetadev merged 4 commits into
ipld:masterfrom
sumanjeet0012:fix-64-prefix-sum-hash-types

Conversation

@sumanjeet0012

@sumanjeet0012 sumanjeet0012 commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Closes #64

Description

This PR addresses the limitation where Prefix.sum() and V1Builder.sum() only supported sha2-256 and sha2-512 by hardcoding hashlib.sha256/512. The hashing logic now correctly delegates to multihash.sum() which supports all registered hash functions in python-multihash.

Changes

  • Updated Prefix.sum() in cid/prefix.py to use multihash.sum() instead of manually invoking hashlib and checking mh_type.
  • Updated V0Builder.sum() and V1Builder.sum() in cid/builder.py similarly to delegate to multihash.sum().
  • Removed unnecessary hashlib imports from both modules.
  • Added tests in tests/test_prefix.py to ensure various multihash types like sha3-256, blake2b-256, and identity are supported.

@sumanjeet0012
sumanjeet0012 force-pushed the fix-64-prefix-sum-hash-types branch from 7efbc8d to 221efe7 Compare July 12, 2026 18:53
@seetadev

Copy link
Copy Markdown
Contributor

Thanks for another excellent contribution, @sumanjeet0012.

This is a clean and well-designed improvement that makes py-cid more flexible and better aligned with the multiformats ecosystem. Instead of hardcoding support for only sha2-256 and sha2-512, delegating the hashing logic to multihash.sum() is the right abstraction, allowing Prefix.sum() and the builder APIs to automatically support the full set of registered multihash algorithms.

This not only simplifies the implementation by removing duplicate hashing logic, but also ensures future compatibility as additional hash functions are supported by python-multihash.

I also appreciate the accompanying test coverage for algorithms such as sha3-256, blake2b-256, and identity, which gives good confidence that the new behavior works as intended and helps prevent regressions going forward. The implementation is concise, the unnecessary imports have been removed, and the full CI matrix is passing successfully across all supported environments.

Overall, this is a valuable improvement that enhances standards compliance while reducing maintenance overhead.

LGTM, thanks for the thoughtful contribution and happy to merge.

CCing @acul71, @johannamoran and @mishmosh.

@seetadev
seetadev merged commit 307c1e6 into ipld:master Jul 13, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prefix.sum() only supports sha2-256 and sha2-512 — should support all hash types

2 participants