Files
continuwuity/src/core/utils/hash/sha256.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.1 KiB
Rust
Raw Normal View History

2026-04-24 12:56:05 +01:00
use sha2::{Digest, Sha256};
2026-04-24 12:56:05 +01:00
pub type DigestOut = [u8; 256 / 8];
/// Sha256 hash (input gather joined by 0xFF bytes)
#[must_use]
#[tracing::instrument(skip(inputs), level = "trace")]
2026-05-20 14:12:21 -07:00
#[allow(clippy::unnecessary_fallible_conversions)]
2026-04-24 12:56:05 +01:00
pub fn delimited<'a, T, I>(mut inputs: I) -> DigestOut
where
I: Iterator<Item = T> + 'a,
T: AsRef<[u8]> + 'a,
{
2026-04-24 12:56:05 +01:00
let mut ctx = Sha256::new();
if let Some(input) = inputs.next() {
ctx.update(input.as_ref());
for input in inputs {
ctx.update(b"\xFF");
ctx.update(input.as_ref());
}
}
2026-04-25 10:07:05 +01:00
ctx.finalize().into()
}
/// Sha256 hash (input gather)
#[must_use]
#[tracing::instrument(skip(inputs), level = "trace")]
2026-05-20 14:12:21 -07:00
#[allow(clippy::unnecessary_fallible_conversions)]
2026-04-24 12:56:05 +01:00
pub fn concat<'a, T, I>(inputs: I) -> DigestOut
where
I: Iterator<Item = T> + 'a,
T: AsRef<[u8]> + 'a,
{
inputs
2026-04-24 12:56:05 +01:00
.fold(Sha256::new(), |mut ctx, input| {
ctx.update(input.as_ref());
ctx
})
2026-04-24 12:56:05 +01:00
.finalize()
2026-04-25 10:07:05 +01:00
.into()
}
/// Sha256 hash
#[inline]
#[must_use]
#[tracing::instrument(skip(input), level = "trace")]
2026-05-20 14:12:21 -07:00
#[allow(clippy::unnecessary_fallible_conversions)]
2026-04-24 12:56:05 +01:00
pub fn hash<T>(input: T) -> DigestOut
where
T: AsRef<[u8]>,
{
2026-04-25 10:07:05 +01:00
Sha256::digest(input).into()
2024-07-03 00:44:00 +00:00
}