Skip to content

feat: add SHA-2 family of hash functions#1032

Merged
siriak merged 1 commit intoTheAlgorithms:masterfrom
AliAlimohammadi:sha2-hash-functions
Mar 31, 2026
Merged

feat: add SHA-2 family of hash functions#1032
siriak merged 1 commit intoTheAlgorithms:masterfrom
AliAlimohammadi:sha2-hash-functions

Conversation

@AliAlimohammadi
Copy link
Copy Markdown
Contributor

Description

Implements the full SHA-2 family of cryptographic hash functions (FIPS PUB 180-4) as pure-Rust, dependency-free one-shot functions:

Function Digest Word Rounds
sha224 224 b 32 64
sha256 256 b 32 64
sha384 384 b 64 80
sha512 512 b 64 80
sha512_224 224 b 64 80
sha512_256 256 b 64 80

SHA-2 is a family of Merkle–Damgård hash functions with a Davies–Meyer compression step. A 32-bit engine (SHA-256/224) and a 64-bit engine (SHA-512/384/512-224/512-256) share the same structure but differ in word size, number of rounds, initial values, and block size.
Reference: NIST FIPS PUB 180-4

This PR also removes the existing sha256.rs, which provided only a stateful SHA-256 struct. All six SHA-2 variants are now covered in a single sha2.rs module. The hashing_traits.rs test that depended on sha256.rs has been removed along with it, as SHA256 and get_hash_string are no longer part of the public API.

Type of change

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Breaking: sha256.rs is deleted. The SHA256 struct and get_hash_string function it exported are removed. The replacement one-shot sha256(msg: &[u8]) -> [u8; 32] covers the same algorithm.

Checklist

  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

Note on clippy::big_endian_bytes: sha2.rs carries #![allow(clippy::big_endian_bytes)] at the top of the file. This lint is from clippy's restriction group and flags all big-endian byte conversions. SHA-2 is defined by FIPS 180-4 to use big-endian byte order throughout; switching to little-endian would produce wrong digests. The allow is intentional and documented in the file.

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.97%. Comparing base (1c7738e) to head (88159f7).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1032      +/-   ##
==========================================
- Coverage   96.06%   95.97%   -0.10%     
==========================================
  Files         388      388              
  Lines       29468    29427      -41     
==========================================
- Hits        28309    28243      -66     
- Misses       1159     1184      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
Copy Markdown
Contributor Author

@siriak, this is ready to be merged.

@siriak siriak merged commit 3bad194 into TheAlgorithms:master Mar 31, 2026
7 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.

3 participants