pub struct Hash(/* private fields */);Expand description
An output of the default size, 32 bytes, which provides constant-time equality checking.
Hash implements From and Into for [u8; 32], and it provides
from_bytes and as_bytes for explicit conversions between itself and
[u8; 32]. However, byte arrays and slices don’t provide constant-time
equality checking, which is often a security requirement in software that
handles private data. Hash doesn’t implement Deref or AsRef, to
avoid situations where a type conversion happens implicitly and the
constant-time property is accidentally lost.
Hash provides the to_hex and from_hex methods for converting to
and from hexadecimal. It also implements Display and FromStr.
§Trait implementations
Clonereproduces the digest byte for byte.Copyleaves the same digest available at both places.Debugincludes the full digest in hexadecimal, even for a derived key.Displayprints all 32 bytes in lowercase hexadecimal, even for a derived key.Eqtreats digests with identical bytes as equal.Fromconverts between the digest and its 32-byte array without changing the bytes; array equality is not constant-time.FromStrdecodes exactly 64 hexadecimal digits, accepting either case.Hashfeeds the length 32 and then the 32 digest bytes to whatevercore::hash::Hasherthe caller supplies, whose running time may depend on those bytes.PartialEqcompares digests, arrays and slices in constant time for 32-byte operands; a different length is unequal.
Implementations§
Source§impl Hash
impl Hash
Sourcepub const fn as_bytes(&self) -> &[u8; 32]
pub const fn as_bytes(&self) -> &[u8; 32]
The raw bytes of the Hash. Note that byte arrays don’t provide
constant-time equality checking, so if you need to compare hashes,
prefer the Hash type.
Sourcepub const fn from_bytes(bytes: [u8; 32]) -> Hash
pub const fn from_bytes(bytes: [u8; 32]) -> Hash
Create a Hash from its raw bytes representation.
Sourcepub const fn as_slice(&self) -> &[u8] ⓘ
pub const fn as_slice(&self) -> &[u8] ⓘ
The raw bytes of the Hash, as a slice. Useful for serialization. Note that byte arrays
don’t provide constant-time equality checking, so if you need to compare hashes, prefer
the Hash type.
Sourcepub fn from_slice(bytes: &[u8]) -> Result<Hash, TryFromSliceError>
pub fn from_slice(bytes: &[u8]) -> Result<Hash, TryFromSliceError>
Create a Hash from its raw bytes representation as a slice.
Returns an error if the slice is not exactly 32 bytes long.
Sourcepub fn to_hex(&self) -> HexHash
pub fn to_hex(&self) -> HexHash
Encode a Hash in lowercase hexadecimal.
Each character is computed from its four bits by arithmetic: no table is indexed by the hash and no branch is taken on it, so the call’s time and memory accesses are the same for every hash (a hash may be a secret: a keyed hash’s tag, a derived key).
The returned HexHash holds the 64 ASCII hexadecimal bytes inline
with no heap allocation and dereferences to &str. Note that
HexHash doesn’t provide constant-time equality checking, so if you
need to compare hashes, prefer the Hash type.
Sourcepub fn from_hex(hex: impl AsRef<[u8]>) -> Result<Hash, HexError>
pub fn from_hex(hex: impl AsRef<[u8]>) -> Result<Hash, HexError>
Decode a Hash from hexadecimal. Both uppercase and lowercase ASCII
bytes are supported.
Any byte outside the ranges '0'...'9', 'a'...'f', and 'A'...'F'
results in an error. An input length other than 64 also results in an
error.
For 64 hexadecimal characters, the call’s time and memory accesses are the same whatever they are: each is decoded by arithmetic, all 64 always. An input that is refused is then searched for the character the error names.
Note that Hash also implements FromStr, so Hash::from_hex("...")
is equivalent to "...".parse().
Trait Implementations§
impl Copy for Hash
impl Eq for Hash
Source§impl PartialEq for Hash
This implementation is constant-time (branchless integer assembly; on Arm
it leaves PSTATE.DIT as the calling thread has it, matching hashing).
impl PartialEq for Hash
This implementation is constant-time (branchless integer assembly; on Arm
it leaves PSTATE.DIT as the calling thread has it, matching hashing).
Source§impl PartialEq<[u8; 32]> for Hash
This implementation is constant-time (branchless integer assembly; on Arm
it leaves PSTATE.DIT as the calling thread has it, matching hashing).
impl PartialEq<[u8; 32]> for Hash
This implementation is constant-time (branchless integer assembly; on Arm
it leaves PSTATE.DIT as the calling thread has it, matching hashing).