#hashing #digest-algorithm #digest #hash #hash-values

no-std sha2

Pure Rust implementation of the SHA-2 hash function family including SHA-224, SHA-256, SHA-384, and SHA-512

27 releases

0.11.0-pre.4 Jul 26, 2024
0.11.0-pre.3 Feb 1, 2024
0.11.0-pre.2 Jan 17, 2024
0.10.8 Sep 26, 2023
0.1.2 May 6, 2016

#107 in Cryptography

Download history 2077522/week @ 2024-07-30 2095591/week @ 2024-08-06 2057111/week @ 2024-08-13 2101020/week @ 2024-08-20 2004242/week @ 2024-08-27 2138366/week @ 2024-09-03 2041613/week @ 2024-09-10 2240530/week @ 2024-09-17 2336905/week @ 2024-09-24 3026909/week @ 2024-10-01 3396657/week @ 2024-10-08 3220445/week @ 2024-10-15 2370433/week @ 2024-10-22 2196820/week @ 2024-10-29 2156220/week @ 2024-11-05 2228913/week @ 2024-11-12

9,499,767 downloads per month
Used in 20,252 crates (3,682 directly)

MIT/Apache

88KB
2K SLoC

RustCrypto: SHA-2

crate Docs Apache2/MIT licensed Rust Version Project Chat Build Status

Pure Rust implementation of the SHA-2 cryptographic hash algorithms.

There are 6 standard algorithms specified in the SHA-2 standard: Sha224, Sha256, Sha512_224, Sha512_256, Sha384, and Sha512.

Algorithmically, there are only 2 core algorithms: SHA-256 and SHA-512. All other algorithms are just applications of these with different initial hash values, and truncated to different digest bit lengths. The first two algorithms in the list are based on SHA-256, while the last four are based on SHA-512.

Examples

One-shot API

use sha2::{Sha256, Digest};
use hex_literal::hex;

let hash = Sha256::digest(b"hello world");
assert_eq!(hash, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"));

// Hex-encode hash using https://docs.rs/base16ct
let hex_hash = base16ct::lower::encode_string(&hash);
assert_eq!(hex_hash, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");

Incremental API

use sha2::{Sha256, Sha512, Digest};
use hex_literal::hex;

let mut hasher = Sha256::new();
hasher.update(b"hello ");
hasher.update(b"world");
let hash256 = hasher.finalize();

assert_eq!(hash256, hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"));

let mut hasher = Sha512::new();
hasher.update(b"hello world");
let hash512 = hasher.finalize();

assert_eq!(hash512, hex!(
    "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f"
    "989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"
));

Also, see the examples section in the RustCrypto/hashes readme.

Minimum Supported Rust Version

Rust 1.72 or higher.

Minimum supported Rust version can be changed in the future, but it will be done with a minor version bump.

SemVer Policy

  • All on-by-default features of this library are covered by SemVer
  • MSRV is considered exempt from SemVer as noted above

License

The crate is licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~320–540KB
~12K SLoC