Skip to content

Blake2bDigest: defensive improvements and cleanup#2205

Closed
lomigmegard wants to merge 1 commit intobcgit:mainfrom
lomigmegard:improve/blake2b-digest-cleanup
Closed

Blake2bDigest: defensive improvements and cleanup#2205
lomigmegard wants to merge 1 commit intobcgit:mainfrom
lomigmegard:improve/blake2b-digest-cleanup

Conversation

@lomigmegard
Copy link
Contributor

While looking at Blake2bDigest implementation I noticed a few things that could be tidied up.

Defensive changes:

  • Validate key length before copying to fail fast and avoid unnecessary allocation on invalid input
  • Centralize state cleanup in reset() to avoid double-zeroing and ensure consistent cleanup path

Code quality:

  • Use explicit 64-bit literals for long field f1 for clarity. I initially thought it was a bug, but Java bit extension makes the current syntax correct but misleading.
  • Other trivial cleanup

LLM disclaimer:

  • A human, myself, wrote all changes by hand
  • A LLM reviewed the changes and created the commit message

Defensive changes:
- Validate key length before copying to fail fast and avoid
  unnecessary allocation on invalid input
- Add null check before zeroing chainValue in reset()
- Centralize state cleanup in reset() to avoid double-zeroing
  and ensure consistent cleanup path

Code quality:
- Use explicit 64-bit literals for long fields (f1) for clarity
- Add final modifier to ROUNDS constant
- Remove redundant return statement

Minor fixes:
- Fix typo in Javadoc (Blakbe2b -> Blake2b)
- Use more efficient Pack.longToLittleEndian overload for
  partial digest output, avoiding intermediate byte[] allocation
@peterdettman peterdettman self-assigned this Dec 2, 2025
@peterdettman
Copy link
Collaborator

Merged, thanks for the PR.

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.

2 participants