pub struct Checksum<D: Digest> {
digest: Vec<u8>,
_marker: PhantomData<D>,
}Expand description
A checksum using a supported algorithm
Checksums are created using one of the supported algorithms:
Blake2b512Md5(WARNING: Use of this algorithm is highly discouraged, because it is cryptographically unsafe)Sha1(WARNING: Use of this algorithm is highly discouraged, because it is cryptographically unsafe)Sha224Sha256Sha384Sha512Crc32Cksum(WARNING: Use of this algorithm is highly discouraged, because it is cryptographically unsafe)
§Note
There are two ways to use a checksum:
- Generically over a digest (e.g.
Checksum::<Blake2b512>) - Using the convenience type aliases (e.g.
Blake2b512Checksum)
§Examples
use std::str::FromStr;
use alpm_types::{digests::Blake2b512, Checksum};
let checksum = Checksum::<Blake2b512>::calculate_from("foo\n");
let digest = vec![
210, 2, 215, 149, 29, 242, 196, 183, 17, 202, 68, 180, 188, 201, 215, 179, 99, 250, 66,
82, 18, 126, 5, 140, 26, 145, 14, 192, 91, 108, 208, 56, 215, 28, 194, 18, 33, 192, 49,
192, 53, 159, 153, 62, 116, 107, 7, 245, 150, 92, 248, 197, 195, 116, 106, 88, 51, 122,
217, 171, 101, 39, 142, 119,
];
assert_eq!(checksum.inner(), digest);
assert_eq!(
format!("{}", checksum),
"d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e77",
);
// create checksum from hex string
let checksum = Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e77")?;
assert_eq!(checksum.inner(), digest);§Developer Note
In case you want to wrap this type and make the parent Serializeable, please note the
following:
Serde automatically adds a Serialize trait bound on top of it trait bounds in wrapper
types. However, that’s not needed as we use D simply as a phantom marker that
isn’t serialized in the first place.
To fix this in your wrapper type, make use of the [bound container attribute], e.g.:
use alpm_types::{Checksum, digests::Digest};
use serde::Serialize;
#[derive(Serialize)]
struct Wrapper<D: Digest> {
#[serde(bound = "D: Digest")]
checksum: Checksum<D>,
}Fields§
§digest: Vec<u8>§_marker: PhantomData<D>Implementations§
Source§impl<D: DigestString> Checksum<D>
impl<D: DigestString> Checksum<D>
Sourcepub fn calculate_from(input: impl AsRef<[u8]>) -> Self
pub fn calculate_from(input: impl AsRef<[u8]>) -> Self
Calculate a new Checksum for data that may be represented as a list of bytes
§Examples
use alpm_types::{digests::Blake2b512, Checksum};
assert_eq!(
format!("{}", Checksum::<Blake2b512>::calculate_from("foo\n")),
"d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e77",
);Sourcepub fn calculate_from_reader(reader: impl Read) -> Result<Self, Error>
pub fn calculate_from_reader(reader: impl Read) -> Result<Self, Error>
Calculates a new Checksum by streaming data from a reader.
Unlike Checksum::calculate_from, this does not require holding the entire input in
memory.
§Errors
Returns an error if reading from reader fails.
§Examples
use alpm_types::{Checksum, digests::Sha256};
let reader = "foo\n".as_bytes();
let checksum = Checksum::<Sha256>::calculate_from_reader(reader)?;
assert_eq!(
format!("{}", checksum),
"b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
);Trait Implementations§
Source§impl<D: DigestString> AlpmParser for Checksum<D>
impl<D: DigestString> AlpmParser for Checksum<D>
Source§impl<D: DigestString> Debug for Checksum<D>
Use Display as Debug impl, since the byte representation and PhantomData field aren’t
relevant for debugging purposes.
impl<D: DigestString> Debug for Checksum<D>
Use Display as Debug impl, since the byte representation and PhantomData field aren’t relevant for debugging purposes.
Source§impl<'de, D: DigestString> Deserialize<'de> for Checksum<D>
Available on crate feature serde only.
impl<'de, D: DigestString> Deserialize<'de> for Checksum<D>
serde only.Source§fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>where
De: Deserializer<'de>,
fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>where
De: Deserializer<'de>,
Source§impl<D: DigestString> Display for Checksum<D>
impl<D: DigestString> Display for Checksum<D>
impl<D: Digest> Eq for Checksum<D>
Source§impl<D: Digest> From<Array<u8, <D as OutputSizeUser>::OutputSize>> for Checksum<D>
impl<D: Digest> From<Array<u8, <D as OutputSizeUser>::OutputSize>> for Checksum<D>
Source§fn from(digest: Output<D>) -> Self
fn from(digest: Output<D>) -> Self
Creates a Checksum from the output of a finalized hash function.
This allows the creation of alpm Checksums from the underlying digest types.
§Examples
use alpm_types::{Checksum, digests::Sha256};
use digest::Digest;
let mut hasher = Sha256::new();
hasher.update("foo\n");
let checksum: Checksum<Sha256> = hasher.finalize().into();
assert_eq!(
format!("{}", checksum),
"b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
);Source§impl<D: DigestString> FromStr for Checksum<D>
impl<D: DigestString> FromStr for Checksum<D>
Source§fn from_str(s: &str) -> Result<Checksum<D>, Self::Err>
fn from_str(s: &str) -> Result<Checksum<D>, Self::Err>
Create a new Checksum from a hex string and return it in a Result
The input is processed as a lowercase string. An Error is returned, if the input length does not match the output size for the given supported algorithm, or if the provided hex string could not be converted to a list of bytes.
Delegates to Checksum::parser.
§Examples
use std::str::FromStr;
use alpm_types::{digests::Blake2b512, Checksum};
assert!(Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e77").is_ok());
assert!(Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e7").is_err());
assert!(Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e7x").is_err());Source§impl<D: Digest> Ord for Checksum<D>
impl<D: Digest> Ord for Checksum<D>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<D: Digest> PartialOrd for Checksum<D>
impl<D: Digest> PartialOrd for Checksum<D>
Source§impl<D: DigestString> Serialize for Checksum<D>
Available on crate feature serde only.
impl<D: DigestString> Serialize for Checksum<D>
serde only.Auto Trait Implementations§
impl<D> Freeze for Checksum<D>
impl<D> RefUnwindSafe for Checksum<D>where
D: RefUnwindSafe,
impl<D> Send for Checksum<D>where
D: Send,
impl<D> Sync for Checksum<D>where
D: Sync,
impl<D> Unpin for Checksum<D>where
D: Unpin,
impl<D> UnsafeUnpin for Checksum<D>
impl<D> UnwindSafe for Checksum<D>where
D: UnwindSafe,
Blanket Implementations§
§impl<T> AnyEq for T
impl<T> AnyEq for T
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<U> ParserUntil for Uwhere
U: AlpmParser,
impl<U> ParserUntil for Uwhere
U: AlpmParser,
Source§fn parser_until<'a, P>(
delimiter: P,
) -> impl Parser<&'a str, U, ErrMode<ContextError>>
fn parser_until<'a, P>( delimiter: P, ) -> impl Parser<&'a str, U, ErrMode<ContextError>>
Returns a [Parser] that parses Self until the given delimiter parser
matches.
§Note
Does not consume the delimiter.
Source§fn parser_until_eof(input: &mut &str) -> Result<Self, ErrMode<ContextError>>
fn parser_until_eof(input: &mut &str) -> Result<Self, ErrMode<ContextError>>
Source§impl<U> ParserUntilInclusive for Uwhere
U: ParserUntil,
impl<U> ParserUntilInclusive for Uwhere
U: ParserUntil,
Source§fn parser_until_inclusive<'a, P>(
delimiter: P,
) -> impl Parser<&'a str, U, ErrMode<ContextError>>
fn parser_until_inclusive<'a, P>( delimiter: P, ) -> impl Parser<&'a str, U, ErrMode<ContextError>>
Returns a [Parser] that parses the whole input and consumes the given delimiter parser.
Delegates to ParserUntil::parser_until and consumes the delimiter.