pub struct Digest(/* private fields */);
Expand description
A cryptographically secure digest, implemented with SHA-256.
A Digest
represents the cryptographic hash of some data. In this
implementation, SHA-256 is used, which produces a 32-byte hash value.
Digests are used throughout the crate for data verification and as unique
identifiers derived from data.
§CBOR Serialization
Digest
implements the CBORTaggedCodable
trait, which means it can be
serialized to and deserialized from CBOR with a specific tag. The tag used
is TAG_DIGEST
defined in the tags
module.
§UR Serialization
When serialized as a Uniform Resource (UR), a Digest
is represented as a
binary blob with the type “digest”.
§Examples
Creating a digest from data:
use bc_components::Digest;
// Create a digest from a string
let data = "hello world";
let digest = Digest::from_image(data.as_bytes());
// Validate that the digest matches the original data
assert!(digest.validate(data.as_bytes()));
Creating and using a digest with hexadecimal representation:
use bc_components::Digest;
// Create a digest from a hex string
let hex_string =
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
let digest = Digest::from_hex(hex_string);
// Retrieve the digest as hex
assert_eq!(digest.hex(), hex_string);
Implementations§
Source§impl Digest
impl Digest
pub const DIGEST_SIZE: usize = 32usize
Sourcepub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
pub fn from_data_ref(data: impl AsRef<[u8]>) -> Result<Self>
Create a new digest from data.
Returns None
if the data is not the correct length.
Sourcepub fn from_image(image: impl AsRef<[u8]>) -> Self
pub fn from_image(image: impl AsRef<[u8]>) -> Self
Create a new digest from the given image.
The image is hashed with SHA-256.
Sourcepub fn from_image_parts(image_parts: &[&[u8]]) -> Self
pub fn from_image_parts(image_parts: &[&[u8]]) -> Self
Create a new digest from an array of data items.
The image parts are concatenated and hashed with SHA-256.
Sourcepub fn from_digests(digests: &[Digest]) -> Self
pub fn from_digests(digests: &[Digest]) -> Self
Create a new digest from an array of Digests.
The image parts are concatenated and hashed with SHA-256.
Sourcepub fn validate(&self, image: impl AsRef<[u8]>) -> bool
pub fn validate(&self, image: impl AsRef<[u8]>) -> bool
Validate the digest against the given image.
The image is hashed with SHA-256 and compared to the digest.
Returns true
if the digest matches the image.
Sourcepub fn from_hex(hex: impl AsRef<str>) -> Self
pub fn from_hex(hex: impl AsRef<str>) -> Self
Create a new digest from the given hexadecimal string.
§Panics
Panics if the string is not exactly 64 hexadecimal digits.
Sourcepub fn short_description(&self) -> String
pub fn short_description(&self) -> String
The first four bytes of the digest as a hexadecimal string.
Trait Implementations§
Source§impl AsRef<Digest> for Digest
Provides a self-reference, enabling API consistency with other types.
impl AsRef<Digest> for Digest
Provides a self-reference, enabling API consistency with other types.
Source§impl CBORTagged for Digest
Identifies the CBOR tags used for Digest serialization.
impl CBORTagged for Digest
Identifies the CBOR tags used for Digest serialization.
Source§impl CBORTaggedDecodable for Digest
Defines how a Digest is decoded from CBOR.
impl CBORTaggedDecodable for Digest
Defines how a Digest is decoded from CBOR.
Source§fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
fn from_untagged_cbor(cbor: CBOR) -> Result<Self>
Source§fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
fn from_tagged_cbor(cbor: CBOR) -> Result<Self, Error>where
Self: Sized,
Source§impl CBORTaggedEncodable for Digest
Defines how a Digest is encoded as CBOR (as a byte string).
impl CBORTaggedEncodable for Digest
Defines how a Digest is encoded as CBOR (as a byte string).
Source§fn untagged_cbor(&self) -> CBOR
fn untagged_cbor(&self) -> CBOR
Source§fn tagged_cbor(&self) -> CBOR
fn tagged_cbor(&self) -> CBOR
Source§impl DigestProvider for Digest
Implements DigestProvider to return itself without copying, as a Digest is
already a digest.
impl DigestProvider for Digest
Implements DigestProvider to return itself without copying, as a Digest is already a digest.
Source§impl Display for Digest
Provides a string representation showing the digest’s hexadecimal value.
impl Display for Digest
Provides a string representation showing the digest’s hexadecimal value.
Source§impl<'a> From<&'a Digest> for &'a [u8]
Allows accessing the underlying data as a byte slice reference.
impl<'a> From<&'a Digest> for &'a [u8]
Allows accessing the underlying data as a byte slice reference.
Source§impl<'a> From<&'a Digest> for &'a [u8; 32]
Allows accessing the underlying data as a fixed-size byte array reference.
impl<'a> From<&'a Digest> for &'a [u8; 32]
Allows accessing the underlying data as a fixed-size byte array reference.
Source§impl From<&Digest> for Vec<u8>
Converts a Digest reference into a Vec<u8>
containing the digest bytes.
impl From<&Digest> for Vec<u8>
Converts a Digest reference into a Vec<u8>
containing the digest bytes.
Source§impl Ord for Digest
Enables total ordering of Digests by comparing their underlying bytes
lexicographically.
impl Ord for Digest
Enables total ordering of Digests by comparing their underlying bytes lexicographically.
Source§impl PartialOrd for Digest
Enables partial ordering of Digests by comparing their underlying bytes.
impl PartialOrd for Digest
Enables partial ordering of Digests by comparing their underlying bytes.
Source§impl TryFrom<CBOR> for Digest
Enables conversion from CBOR to Digest, with proper error handling.
impl TryFrom<CBOR> for Digest
Enables conversion from CBOR to Digest, with proper error handling.
impl Eq for Digest
impl StructuralPartialEq for Digest
Auto Trait Implementations§
impl Freeze for Digest
impl RefUnwindSafe for Digest
impl Send for Digest
impl Sync for Digest
impl Unpin for Digest
impl UnwindSafe for Digest
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> CBORDecodable for T
impl<T> CBORDecodable for T
Source§impl<T> CBOREncodable for T
impl<T> CBOREncodable for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Lower case
letters are used (e.g. f9b4ca
)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self
into the result. Upper case
letters are used (e.g. F9B4CA
)