Expand description
The SHA-2 cryptographic hash functions.
sha2
provides implementations of the SHA-2 family of cryptographic hash functions.
The SHA-2 family includes six hash functions: SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and SHA-512/256. These are cryptographic hash functions standardized by NIST, producing fixed-size digests from arbitrary input data. SHA-256 and SHA-512 are the most commonly used variants.
The crate implements the Digest
trait,
providing both one-shot hashing via Digest::digest
and incremental hashing via Digest::new
, Digest::update
, and Digest::finalize
.
§Examples
Basic SHA-256 hashing:
use sha2::{Sha256, Digest};
let result = Sha256::digest(b"hello world");
println!("SHA-256: {:x}", result);
Incremental hashing:
use sha2::{Sha256, Digest};
let mut hasher = Sha256::new();
hasher.update(b"hello ");
hasher.update(b"world");
let result = hasher.finalize();
// Verify it matches one-shot hashing
assert_eq!(result[..], Sha256::digest(b"hello world")[..]);
Using different SHA-2 variants:
use sha2::{Sha512, Digest};
let sha512_result = Sha512::digest(b"hello world");
println!("SHA-512: {:x}", sha512_result);
Modules§
- digest
- This crate provides traits which describe functionality of cryptographic hash functions and Message Authentication algorithms.
Structs§
- Sha256
VarCore - Core block-level SHA-256 hasher with variable output size.
- Sha512
VarCore - Core block-level SHA-512 hasher with variable output size.
Traits§
- Digest
- Convenience wrapper trait covering functionality of cryptographic hash functions with fixed output size.
Type Aliases§
- Sha224
- SHA-224 hasher.
- Sha256
- SHA-256 hasher.
- Sha384
- SHA-384 hasher.
- Sha512
- SHA-512 hasher.
- Sha512_
224 - SHA-512/224 hasher.
- Sha512_
256 - SHA-512/256 hasher.