Skip to main content

auths_crypto/
hash256.rs

1//! Typed newtype for 32-byte hash digests.
2//!
3//! `Hash256` is a generic 32-byte digest container — SHA-256, Blake3, or any
4//! 32-byte content address. It is NOT a signing key and must not be typed as
5//! one. This newtype disambiguates at the type level so the Rust compiler
6//! distinguishes hash values from Ed25519 signing-key byte arrays.
7//!
8//! Wire format is unchanged: `#[serde(transparent)]` preserves byte-identical
9//! serialization with the prior bare `[u8; 32]`.
10
11use serde::{Deserialize, Serialize};
12
13/// 32-byte hash digest (SHA-256, Blake3, or any content address).
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
15#[serde(transparent)]
16pub struct Hash256([u8; 32]);
17
18impl Hash256 {
19    /// Wrap a raw 32-byte digest.
20    pub const fn new(bytes: [u8; 32]) -> Self {
21        Self(bytes)
22    }
23
24    /// Borrow the underlying bytes.
25    pub fn as_bytes(&self) -> &[u8; 32] {
26        &self.0
27    }
28
29    /// Consume into the underlying bytes.
30    pub fn into_inner(self) -> [u8; 32] {
31        self.0
32    }
33}
34
35impl From<[u8; 32]> for Hash256 {
36    fn from(bytes: [u8; 32]) -> Self {
37        Self(bytes)
38    }
39}
40
41impl AsRef<[u8]> for Hash256 {
42    fn as_ref(&self) -> &[u8] {
43        &self.0
44    }
45}