Skip to main content

compmd_pack/
algorithm.rs

1//! A hash's algorithm, as an integrity names it.
2
3/// A hash's algorithm.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub enum Algorithm {
6    /// SHA-256.
7    Sha256,
8    /// SHA-384, which a frame checks a built module against.
9    Sha384,
10    /// SHA-512, which npm publishes for a release's tarball (spec ยง6.1).
11    Sha512,
12}
13
14impl Algorithm {
15    /// Every algorithm an integrity may name.
16    pub const ALL: [Self; 3] = [Self::Sha256, Self::Sha384, Self::Sha512];
17
18    /// Its name in an integrity: `sha256`, `sha384`, `sha512`.
19    #[must_use]
20    pub const fn name(self) -> &'static str {
21        match self {
22            Self::Sha256 => "sha256",
23            Self::Sha384 => "sha384",
24            Self::Sha512 => "sha512",
25        }
26    }
27}