Skip to content

Add BLAKE2 b2sum support#92

Open
sarsolot wants to merge 1 commit intocfv-project:python3from
sarsolot:feature/blake2
Open

Add BLAKE2 b2sum support#92
sarsolot wants to merge 1 commit intocfv-project:python3from
sarsolot:feature/blake2

Conversation

@sarsolot
Copy link
Copy Markdown
Contributor

Closes #9

Summary

Add BLAKE2 support as a single b2 type, following the same shape as b3 (#88).

  • Single b2 type with --length for variable digest size in bits (default 512, max 512)
  • Uses Python's built-in hashlib.blake2b - no external dependency
  • Recognized extensions: .b2 (default), .blk (TurboSFV compat), B2SUMS filename
  • Interoperable with GNU b2sum (two-space format, verified in tests)

Design decisions

  • BLAKE2b only, no BLAKE2s. Matches b2sum, which is also blake2b-only. If demand arises, blake2s can be added as a separate type later.
  • --length in bits, consistent with b2sum -l and the approach adopted for b3. Validated as a multiple of 8 and against blake2b's 8-512 bit range.
  • .blk as secondary extension. TurboSFV uses .blk for BLAKE2B files. Kept as auto detect fallback, .b2 is the default for creation.
  • Shared HashSumBase. Extracted from the existing BLAKE3 handler. BLAKE2 and BLAKE3 are now thin subclasses with type specific attributes and an _is_valid_filename hook. Test helpers are also shared (_hashsum_roundtrip_test, _hashsum_fixture_test, _hashsum_tool_compat_test, etc.).
  • Filename gated auto-detection (same safety rationale as b3). Prevents confusion with sha* types that produce identical length hex on content.

Digest length handling

  • Oversized digests in a .b2 file are rejected as a malformed checksum line (cferror, exit 64), not treated as file corruption.
  • --length > max_digest_size is validated up-front in make() with a clean --length for b2 must be between 8 and 512 error (exit 1), no traceback.
  • --length not a multiple of 8 is still rejected by the shared _parse_hash_length validator.

Changes

  • lib/cfv/hash.pygetfileblake2b()
  • lib/cfv/common.pyHashSumBase extraction, BLAKE2 class, up-front max_digest_size validation, help text, atrem regex
  • test/test.py — shared _hashsum_* test helpers (replace standalone b3 helpers), b2 in fmt_info, full coverage
  • test/testdata/ — 7 fixtures: test.b2, test.blk (TurboSFV BLAKE2B-512 reference), B2SUMS, test.blake2b-{256,384}.b2, testcrlf.b2, testcrcrlf.b2
  • README.md, cfv.1, Changelog

Add BLAKE2b support as a single b2 type with --length for variable
digest size (default 512 bits, max 512 bits). Uses Python's built-in
hashlib.blake2b, so no external dependency is required.

Extract HashSumBase shared base class from BLAKE3. Both BLAKE2 and
BLAKE3 are now thin subclasses with type-specific attributes and an
_is_valid_filename hook. Refactor the b3_* test helpers into shared
_hashsum_* helpers used by both b2 and b3.

Recognized extensions: .b2 (default), .blk (TurboSFV compat), and the
B2SUMS filename. Interoperable with GNU b2sum (two-space format),
verified in the test suite.

Digest-length handling:
- Oversized digests in .b2 files are rejected as malformed chksum
  lines (cferror exit 64), not treated as file corruption.
- --length > max_digest_size is validated up-front in make() with a
  clean "must be between 8 and 512" error (exit 1), no traceback.
- --length not a multiple of 8 continues to be rejected by the shared
  _parse_hash_length validator.

Fixtures: test.b2, B2SUMS, testcrlf.b2, testcrcrlf.b2 (b2sum-style,
generated), test.blk (TurboSFV BLAKE2B-512 reference), and
test.blake2b-{256,384}.b2 covering non-default digest sizes in both
text and binary b2sum formats.

Closes cfv-project#9.
sarsolot added a commit to sarsolot/cfv that referenced this pull request Apr 14, 2026
Track upstream with BLAKE3 merged (PR cfv-project#88), acknowledge in-flight
BLAKE2 (PR cfv-project#92) and xxhash support. No control changes: Suggests
already lists python3-blake3 and python3-xxhash, blake2 uses hashlib
and needs no new runtime dependency.
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.

Add support for GNU coreutils b2sum files

1 participant