pub struct ContentHasher {
state: u128,
}Expand description
An incremental builder for a deterministic ContentHash over a sequence
of typed fields (design note §4).
Construct with new (which stamps the engine version), fold a
domain tag plus the boundary’s fields with field /
u32, then read the result with finish. The
field sequence is the boundary’s contract: keep it fixed per domain, and
lead with a domain tag so two boundaries that fold identical bytes still
differ.
§Examples
use mos_core::ContentHasher;
// Same fields in the same order hash equal; any change diverges.
let mut a = ContentHasher::new();
a.field(b"example/v1").u32(7).field(b"payload");
let mut b = ContentHasher::new();
b.field(b"example/v1").u32(7).field(b"payload");
assert_eq!(a.finish(), b.finish());
let mut c = ContentHasher::new();
c.field(b"example/v1").u32(8).field(b"payload");
assert_ne!(a.finish(), c.finish());Fields§
§state: u128Implementations§
Source§impl ContentHasher
impl ContentHasher
Sourcepub fn new() -> Self
pub fn new() -> Self
Start a hasher, stamping the engine version (§5 rule 2) so callers cannot forget it.
§Examples
use mos_core::ContentHasher;
// A fresh hasher already carries the engine-version stamp, so two
// freshly-constructed hashers agree before any field is folded.
assert_eq!(ContentHasher::new().finish(), ContentHasher::new().finish());Sourcepub fn field(&mut self, bytes: &[u8]) -> &mut Self
pub fn field(&mut self, bytes: &[u8]) -> &mut Self
Fold one variable-length field, length-prefixed so field boundaries stay unambiguous.
The u64 length prefix is fixed-width (not usize) so the hash is
identical on 32- and 64-bit targets.
§Examples
use mos_core::ContentHasher;
// Length framing keeps `("a", "bc")` distinct from `("ab", "c")`.
let mut split_a = ContentHasher::new();
split_a.field(b"a").field(b"bc");
let mut split_b = ContentHasher::new();
split_b.field(b"ab").field(b"c");
assert_ne!(split_a.finish(), split_b.finish());Sourcepub fn u32(&mut self, value: u32) -> &mut Self
pub fn u32(&mut self, value: u32) -> &mut Self
Fold a fixed-width u32 (little-endian). No length prefix is needed: the
width is constant, so the field boundary is implicit.
§Examples
use mos_core::ContentHasher;
let mut a = ContentHasher::new();
let mut b = ContentHasher::new();
a.u32(1);
b.u32(2);
assert_ne!(a.finish(), b.finish());Sourcepub fn finish(&self) -> ContentHash
pub fn finish(&self) -> ContentHash
The accumulated ContentHash.
§Examples
use mos_core::{ContentHash, ContentHasher};
let hash: ContentHash = ContentHasher::new().field(b"x").finish();
// It is a real 128-bit value, not the default.
assert_ne!(hash, ContentHash::default());Trait Implementations§
Source§impl Clone for ContentHasher
impl Clone for ContentHasher
Source§fn clone(&self) -> ContentHasher
fn clone(&self) -> ContentHasher
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more