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
impl PartialEq<[u8]> for Hash
This implementation is constant-time if the target is 32 bytes long.
source§impl PartialEq<[u8; 32]> for Hash
impl PartialEq<[u8; 32]> for Hash
This implementation is constant-time.
source§impl PartialEq for Hash
impl PartialEq for Hash
This implementation is constant-time.
impl Copy for Hash
impl Eq for Hash
Auto Trait Implementations§
impl Freeze for Hash
impl RefUnwindSafe for Hash
impl Send for Hash
impl Sync for Hash
impl Unpin for Hash
impl UnwindSafe for Hash
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)