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.
A digest is a fixed-size byte array, not a string.
Use the hex crate to render one for display or storage.
§Examples
One-shot SHA-256 hashing:
use sha2::{Digest, Sha256};
let digest = Sha256::digest(b"hello world");
assert_eq!(
hex::encode(digest),
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
);Incremental hashing, for data that does not arrive all at once:
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(b"hello ");
hasher.update(b"world");
let digest = hasher.finalize();
assert_eq!(digest, Sha256::digest(b"hello world"));The variants differ only in digest length and internal state size:
use sha2::{Digest, Sha256, Sha384, Sha512};
assert_eq!(Sha256::digest(b"hello world").len(), 32);
assert_eq!(Sha384::digest(b"hello world").len(), 48);
assert_eq!(Sha512::digest(b"hello world").len(), 64);Modules§
Structs§
- 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.
Traits§
- Digest
- Convenience wrapper trait covering functionality of cryptographic hash functions with fixed output size.