Struct alpm_types::Checksum

source ·
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.

§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>

source

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",
);
source

pub fn inner(&self) -> &[u8]

Return a reference to the inner type

Trait Implementations§

source§

impl<D: Clone + Digest> Clone for Checksum<D>

source§

fn clone(&self) -> Checksum<D>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<D: Debug + Digest> Debug for Checksum<D>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<D: Digest> Display for Checksum<D>

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<D: Digest> FromStr for Checksum<D>

source§

fn from_str(input: &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());
§

type Err = Error

The associated error which can be returned from parsing.

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.