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
.
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]) -> Self
pub const fn from_bytes(bytes: [u8; 32]) -> Self
Create a Hash
from its raw bytes representation.
Sourcepub fn to_hex(&self) -> ArrayString<{ _ }>
pub fn to_hex(&self) -> ArrayString<{ _ }>
Encode a Hash
in lowercase hexadecimal.
The returned ArrayString
is a fixed size and doesn’t allocate memory
on the heap. Note that ArrayString
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<Self, HexError>
pub fn from_hex(hex: impl AsRef<[u8]>) -> Result<Self, 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.
Note that Hash
also implements FromStr
, so Hash::from_hex("...")
is equivalent to "...".parse()
.
Trait Implementations§
Source§impl PartialEq<[u8]> for Hash
This implementation is constant-time if the target is 32 bytes long.
impl PartialEq<[u8]> for Hash
This implementation is constant-time if the target is 32 bytes long.