-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da47a09
commit 446ffd2
Showing
27 changed files
with
511 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,81 @@ | ||
use pypi_types::HashDigest; | ||
use pypi_types::{HashAlgorithm, HashDigest}; | ||
|
||
pub trait Hashed { | ||
/// Return the [`HashDigest`]s for the archive. | ||
fn hashes(&self) -> &[HashDigest]; | ||
|
||
/// Returns `true` if the archive satisfies the given hashes. | ||
fn satisfies(&self, hashes: &[HashDigest]) -> bool { | ||
if hashes.is_empty() { | ||
true | ||
} else { | ||
self.hashes().iter().any(|hash| hashes.contains(hash)) | ||
/// Returns `true` if the archive satisfies the given hash policy. | ||
fn satisfies(&self, hashes: HashPolicy) -> bool { | ||
match hashes { | ||
HashPolicy::None => true, | ||
HashPolicy::Generate => true, | ||
HashPolicy::Validate(hashes) => self.hashes().iter().any(|hash| hashes.contains(hash)), | ||
} | ||
} | ||
|
||
/// Returns `true` if the archive includes a hash for at least one of the given algorithms. | ||
fn has_digests(&self, hashes: &[HashDigest]) -> bool { | ||
if hashes.is_empty() { | ||
true | ||
} else { | ||
hashes | ||
fn has_digests(&self, hashes: HashPolicy) -> bool { | ||
match hashes { | ||
HashPolicy::None => true, | ||
HashPolicy::Generate => self | ||
.hashes() | ||
.iter() | ||
.any(|hash| hash.algorithm == HashAlgorithm::Sha256), | ||
HashPolicy::Validate(hashes) => hashes | ||
.iter() | ||
.map(HashDigest::algorithm) | ||
.any(|algorithm| self.hashes().iter().any(|hash| hash.algorithm == algorithm)) | ||
.any(|algorithm| self.hashes().iter().any(|hash| hash.algorithm == algorithm)), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub enum HashPolicy<'a> { | ||
/// No hash policy is specified. | ||
None, | ||
/// Hashes should be generated (specifically, a SHA-256 hash), but not validated. | ||
Generate, | ||
/// Hashes should be validated against a pre-defined list of hashes. If necessary, hashes should | ||
/// be generated so as to ensure that the archive is valid. | ||
Validate(&'a [HashDigest]), | ||
} | ||
|
||
impl<'a> HashPolicy<'a> { | ||
/// Returns `true` if the hash policy is `None`. | ||
pub fn is_none(&self) -> bool { | ||
matches!(self, HashPolicy::None) | ||
} | ||
|
||
/// Returns `true` if the hash policy is `Generate`. | ||
pub fn is_generate(&self) -> bool { | ||
matches!(self, HashPolicy::Generate) | ||
} | ||
|
||
/// Returns `true` if the hash policy is `Validate`. | ||
pub fn is_validate(&self) -> bool { | ||
matches!(self, HashPolicy::Validate(_)) | ||
} | ||
|
||
/// Return the algorithms used in the hash policy. | ||
pub fn algorithms(&self) -> Vec<HashAlgorithm> { | ||
match self { | ||
HashPolicy::None => vec![], | ||
HashPolicy::Generate => vec![HashAlgorithm::Sha256], | ||
HashPolicy::Validate(hashes) => { | ||
let mut algorithms = hashes.iter().map(HashDigest::algorithm).collect::<Vec<_>>(); | ||
algorithms.sort(); | ||
algorithms.dedup(); | ||
algorithms | ||
} | ||
} | ||
} | ||
|
||
/// Return the digests used in the hash policy. | ||
pub fn digests(&self) -> &[HashDigest] { | ||
match self { | ||
HashPolicy::None => &[], | ||
HashPolicy::Generate => &[], | ||
HashPolicy::Validate(hashes) => hashes, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.