Skip to main content

Sha256

Struct Sha256 

Source
pub struct Sha256 { /* private fields */ }
Expand description

Streaming SHA-256 hash context (FIPS 180-4, Section 6.2).

Internal state and partial input blocks are automatically zeroized on drop. Once finalized, further Sha256::try_update or Sha256::try_finalize_into calls are rejected until Sha256::reset is called.

Implementations§

Source§

impl Sha256

Source

pub const DIGEST_LEN: usize = 32

Output digest length in bytes (32 bytes / 256 bits).

Source

pub const BLOCK_LEN: usize = 64

Compression block size in bytes (64 bytes / 512 bits).

Source

pub const MAX_MESSAGE_BYTES: u64

Maximum supported message length in bytes (2^61 - 1, matching FIPS 180-4 §5.1.1’s strict < 2^64 bits ceiling without bit-counter wraparound).

Source

pub const fn new() -> Self

Creates a new SHA-256 hasher initialized to FIPS 180-4 Section 5.3.3 H^(0).

Source

pub fn reset(&mut self)

Resets this hasher in place to FIPS 180-4 Section 5.3.3 H^(0) after zeroizing prior state.

Source

pub fn try_update(&mut self, data: &[u8]) -> Result<()>

Absorbs data into the SHA-256 compression state, checking post-finalize misuse and the FIPS 180-4 Section 5.1.1 < 2^64 bits (MAX_MESSAGE_BYTES) ceiling.

Source

pub fn update(&mut self, data: &[u8])

Absorbs data into the SHA-256 compression state.

Prefer Self::try_update when callers need fallible checking of the 2^61 - 1 byte FIPS 180-4 message limit or post-finalize misuse.

Source

pub fn try_finalize_into(&mut self, out: &mut [u8; 32]) -> Result<()>

Finalizes the digest directly into out, zeroizes internal state, and marks the hasher finalized so subsequent updates or double-finalizations fail until Self::reset.

Source

pub fn finalize_into(&mut self, out: &mut [u8; 32])

Finalizes the digest directly into out and marks the hasher finalized.

If already finalized or overflowed, zeroes out without emitting stale state.

Source

pub fn finalize(self) -> [u8; 32]

Finalizes the hash and returns the 32-byte SHA-256 digest.

For secret-derived digests, prefer Self::finalize_into or Self::digest_into to write directly into caller-owned zeroized storage without an intermediate return copy.

Source

pub fn digest_into(data: &[u8], out: &mut [u8; 32])

Computes SHA-256(data) directly into out without a by-value digest copy.

Source

pub fn digest_chunks_into(chunks: &[&[u8]], out: &mut [u8; 32])

Computes SHA-256(chunks[0] || chunks[1] || ...) directly into out.

Source

pub fn digest(data: &[u8]) -> [u8; 32]

Computes SHA-256(data) in one shot.

Source

pub fn digest_chunks(chunks: &[&[u8]]) -> [u8; 32]

Computes SHA-256(chunks[0] || chunks[1] || ...) in one shot.

Source

pub fn self_test() -> Result<()>

Executes the FIPS 140-3 known-answer self-test (CAST) for SHA-256.

Verifies both a single-block input ("abc", FIPS 180-4) and a two-block input ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56 bytes) in constant time.

Trait Implementations§

Source§

impl Debug for Sha256

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sha256

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Sha256

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Zeroize for Sha256

Source§

fn zeroize(&mut self)

Overwrites all sensitive memory owned by self with zeros.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.