Struct Checksum

Source
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:

  • Blake2b512
  • Md5 (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)
  • Sha224
  • Sha256
  • Sha384
  • Sha512

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:

  1. Generically over a digest (e.g. Checksum::<Blake2b512>)
  2. 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: 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

Source

pub fn parser(input: &mut &str) -> ModalResult<Self>

Recognizes an ASCII hexadecimal Checksum from a string slice.

Consumes all input. See Checksum::from_str.

§Errors

Returns an error if input is not the output of a hash function in hexadecimal form.

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: Digest> Debug for Checksum<D>

Use Display as Debug impl, since the byte representation and PhantomData field aren’t relevant for debugging purposes.

Source§

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

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

impl<'de, D: Digest> Deserialize<'de> for Checksum<D>

Source§

fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>
where De: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. 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(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§

type Err = Error

The associated error which can be returned from parsing.
Source§

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

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize a Checksum into a hex String representation.

We chose hex as byte vectors are imperformant and considered bad practice for non-binary formats like JSON or YAML

Source§

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

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> UnwindSafe for Checksum<D>
where D: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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§

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

Source§

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

Source§

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T