Skip to content

Fix: validate multihash in CIDv0 constructor (fixes #63)#74

Open
sumanjeet0012 wants to merge 2 commits into
ipld:masterfrom
sumanjeet0012:fix-63-cidv0-multihash-validation
Open

Fix: validate multihash in CIDv0 constructor (fixes #63)#74
sumanjeet0012 wants to merge 2 commits into
ipld:masterfrom
sumanjeet0012:fix-63-cidv0-multihash-validation

Conversation

@sumanjeet0012

Copy link
Copy Markdown

Description

This PR fixes issue #63 by adding validation to the CIDv0 constructor to ensure the provided multihash uses the sha2-256 algorithm and has a 32-byte digest, as required by the CID specification.

Changes

  • Updated CIDv0.__init__ to decode and validate the multihash code (must be 0x12 for sha2-256) and length (must be 32). Raises a ValueError if the multihash is invalid.
  • Added tests test_cidv0_rejects_non_sha256 and test_cidv0_rejects_truncated_digest in tests/test_cid.py.
  • Fixed existing tests in test_cid.py and test_new_features.py that were improperly creating CIDv0 objects using non-compliant hashes or raw byte strings (which now fail due to the new strict validation).

@sumanjeet0012 sumanjeet0012 force-pushed the fix-63-cidv0-multihash-validation branch from 5d1e9f4 to 5ed18b1 Compare July 12, 2026 16:03
@sumanjeet0012 sumanjeet0012 force-pushed the fix-63-cidv0-multihash-validation branch from bfe9ff5 to 76d1c8b Compare July 12, 2026 18:53
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.

1 participant