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
impl Integrity
Sourcepub fn pick_algorithm(&self) -> Algorithm
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);
Sourcepub fn from<B>(data: B) -> Integrity
pub fn from<B>(data: B) -> Integrity
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());
Sourcepub fn from_hex<B>(hex: B, algorithm: Algorithm) -> Result<Integrity, Error>
pub fn from_hex<B>(hex: B, algorithm: Algorithm) -> Result<Integrity, Error>
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);
Sourcepub fn concat(&self, other: Integrity) -> Integrity
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());
Sourcepub fn to_hex(&self) -> (Algorithm, String)
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());
Sourcepub fn matches(&self, other: &Integrity) -> Option<Algorithm>
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<'de> Deserialize<'de> for Integrity
impl<'de> Deserialize<'de> for Integrity
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Integrity, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Integrity, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl FromStr for Integrity
impl FromStr for Integrity
Source§impl Serialize for Integrity
impl Serialize for Integrity
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl Eq for Integrity
impl StructuralPartialEq for Integrity
Auto Trait Implementations§
impl Freeze for Integrity
impl RefUnwindSafe for Integrity
impl Send for Integrity
impl Sync for Integrity
impl Unpin for Integrity
impl UnwindSafe for Integrity
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§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more