pub struct Checksum<D: Digest> {
digest: Vec<u8>,
_marker: PhantomData<*const D>,
}
Expand description
A checksum using a supported algorithm
Checksums are created using one of the supported algorithms:
Blake2b512
Md5
(WARNING: Use is highly discouraged, because it is cryptographically unsafe)Sha1
(WARNING: Use is highly discouraged, because it is cryptographically unsafe)Sha224
Sha256
Sha384
Sha512
NOTE: Contrary to makepkg/pacman, this crate does not support using cksum-style CRC-32 as it is non-standard (different implementations throughout libraries) and 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").unwrap();
assert_eq!(checksum.inner(), digest);
Fields§
§digest: Vec<u8>
§_marker: PhantomData<*const D>
Implementations§
source§impl<D: Digest> Checksum<D>
impl<D: Digest> 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",
);
Trait Implementations§
source§impl<D: Digest> FromStr for Checksum<D>
impl<D: Digest> 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
All whitespaces are removed from the input and it 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.
§Examples
use std::str::FromStr;
use alpm_types::{digests::Blake2b512, Checksum};
assert!(Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e77").is_ok());
assert!(Checksum::<Blake2b512>::from_str("d2 02 d7 95 1d f2 c4 b7 11 ca 44 b4 bc c9 d7 b3 63 fa 42 52 12 7e 05 8c 1a 91 0e c0 5b 6c d0 38 d7 1c c2 12 21 c0 31 c0 35 9f 99 3e 74 6b 07 f5 96 5c f8 c5 c3 74 6a 58 33 7a d9 ab 65 27 8e 77").is_ok());
assert!(Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e7").is_err());
assert!(Checksum::<Blake2b512>::from_str("d202d7951df2c4b711ca44b4bcc9d7b363fa4252127e058c1a910ec05b6cd038d71cc21221c031c0359f993e746b07f5965cf8c5c3746a58337ad9ab65278e7x").is_err());
Auto Trait Implementations§
impl<D> Freeze for Checksum<D>
impl<D> RefUnwindSafe for Checksum<D>where
D: RefUnwindSafe,
impl<D> !Send for Checksum<D>
impl<D> !Sync for Checksum<D>
impl<D> Unpin for Checksum<D>
impl<D> UnwindSafe for Checksum<D>where
D: RefUnwindSafe,
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
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)