Integrity

Struct Integrity 

Source
pub struct Integrity {
    pub hashes: Vec<Hash>,
}
Expand description

Representation of a full Subresource Integrity string.

Integrity can be used for parsing and also includes convenience methods for shorthand versions of IntegrityOpts and IntegrityChecker.

§Example

let source = "sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=";

let parsed: Integrity = source.parse().unwrap();
assert_eq!(parsed.to_string(), source);

Fields§

§hashes: Vec<Hash>

Implementations§

Source§

impl Integrity

Source

pub fn pick_algorithm(&self) -> Algorithm

Pick the most secure available Algorithm in this Integrity.

§Example
use ssri::{Integrity, Algorithm};

let sri: Integrity = "sha1-deadbeef sha256-badc0ffee".parse().unwrap();
let algorithm = sri.pick_algorithm();
assert_eq!(algorithm, Algorithm::Sha256);
Source

pub fn from<B>(data: B) -> Integrity
where B: AsRef<[u8]>,

Create a new Integrity based on data. Use IntegrityOpts for more options.

§Example
use ssri::Integrity;
let sri = Integrity::from(b"hello");
assert_eq!(sri.to_string(), "sha256-LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=".to_owned());
Source

pub fn from_hex<B>(hex: B, algorithm: Algorithm) -> Result<Integrity, Error>
where B: AsRef<[u8]>,

Converts a hex string obtained from to_hex() to an Integrity with a Hash containing algorithm and decoded hex string.

§Example
 use ssri::{Integrity, Algorithm};

 let expected = Integrity::from(b"hello");
 let hex = String::from("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824");
 assert_eq!(Integrity::from_hex(hex, Algorithm::Sha256).unwrap(), expected);
Source

pub fn concat(&self, other: Integrity) -> Integrity

Join together two Integrity instances. Hashes will be grouped and sorted by algorithm but otherwise kept in the same order.

§Example
use ssri::Integrity;
let sri1: Integrity = "sha256-deadbeef".parse().unwrap();
let sri2: Integrity = "sha256-badc0ffee".parse().unwrap();
let sri3 = sri1.concat(sri2);
assert_eq!(sri3.to_string(), "sha256-deadbeef sha256-badc0ffee".to_owned());
Source

pub fn check<B>(&self, data: B) -> Result<Algorithm, Error>
where B: AsRef<[u8]>,

Check some data against this Integrity. For more options, use Checker.

§Example
use ssri::{Algorithm, Integrity};

let sri = Integrity::from(b"hello");
let algorithm = sri.check(b"hello").unwrap();
assert_eq!(algorithm, Algorithm::Sha256);
Source

pub fn to_hex(&self) -> (Algorithm, String)

Converts the first Hash in this Integrity into its hex string format.

§Example
use ssri::{Algorithm, Integrity};

let sri = Integrity::from(b"hello");
let (algo, hex) = sri.to_hex();
assert_eq!(algo, Algorithm::Sha256);
assert_eq!(hex, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".to_owned());
Source

pub fn matches(&self, other: &Integrity) -> Option<Algorithm>

Compares self against a given SRI to see if there’s a match. The deciding algorithm is determined by other.

§Example
use ssri::{Algorithm, Integrity};

let sri1 = Integrity::from(b"hello");
let sri2 = Integrity::from(b"hello").concat(Integrity::from(b"world"));
let m = sri1.matches(&sri2);
assert_eq!(m, Some(Algorithm::Sha256));

Trait Implementations§

Source§

impl Clone for Integrity

Source§

fn clone(&self) -> Integrity

Returns a duplicate 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 Debug for Integrity

Source§

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

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

impl Display for Integrity

Source§

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

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

impl FromStr for Integrity

Source§

fn from_str(s: &str) -> Result<Integrity, <Integrity as FromStr>::Err>

Parses a string into an Integrity instance.

§Example
use ssri::Integrity;
let sri: Integrity = "sha256-deadbeef".parse().unwrap();
assert_eq!(sri.to_string(), String::from("sha256-deadbeef"));
Source§

type Err = Error

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

impl Hash for Integrity

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Integrity

Source§

fn eq(&self, other: &Integrity) -> 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 Eq for Integrity

Source§

impl StructuralPartialEq for Integrity

Auto Trait Implementations§

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.