Skip to main content

ic_backup/model/artifacts/
mod.rs

1//! Canonical SHA-256 artifact metadata; filesystem effects belong to ops.
2
3use ic_host_artifacts::artifact::Sha256Digest;
4use serde::{Deserialize, Serialize};
5use thiserror::Error;
6
7/// Maintained checksum record with a lowercase, validated SHA-256 digest.
8#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
9#[serde(try_from = "ChecksumFields")]
10pub struct ArtifactChecksumRecord {
11    algorithm: String,
12    hash: String,
13}
14
15#[derive(Deserialize)]
16#[serde(deny_unknown_fields)]
17struct ChecksumFields {
18    algorithm: String,
19    hash: String,
20}
21
22impl TryFrom<ChecksumFields> for ArtifactChecksumRecord {
23    type Error = ChecksumError;
24
25    fn try_from(value: ChecksumFields) -> Result<Self, Self::Error> {
26        if value.algorithm != "sha256" {
27            return Err(ChecksumError::UnsupportedAlgorithm(value.algorithm));
28        }
29        Self::from_hash(&value.hash)
30    }
31}
32
33impl ArtifactChecksumRecord {
34    /// Compute checksum metadata from exact bytes.
35    #[must_use]
36    pub fn from_bytes(bytes: &[u8]) -> Self {
37        Self {
38            algorithm: "sha256".to_owned(),
39            hash: Sha256Digest::compute(bytes).to_string(),
40        }
41    }
42
43    /// Validate and normalize an existing digest.
44    ///
45    /// # Errors
46    /// Returns [`ChecksumError::InvalidHash`] unless the input has 64 hex digits.
47    pub fn from_hash(hash: &str) -> Result<Self, ChecksumError> {
48        Ok(Self {
49            algorithm: "sha256".to_owned(),
50            hash: canonical_hash(hash)?,
51        })
52    }
53
54    pub(crate) fn from_digest(digest: [u8; 32]) -> Self {
55        Self {
56            algorithm: "sha256".to_owned(),
57            hash: Sha256Digest::from_bytes(digest).to_string(),
58        }
59    }
60
61    /// Return the maintained algorithm identifier.
62    #[must_use]
63    pub fn algorithm(&self) -> &str {
64        &self.algorithm
65    }
66
67    /// Return the canonical lowercase hexadecimal digest.
68    #[must_use]
69    pub fn hash(&self) -> &str {
70        &self.hash
71    }
72
73    /// Compare a digest, accepting equivalent uppercase and lowercase hex.
74    ///
75    /// # Errors
76    /// Returns a typed malformed-hash or checksum-mismatch error.
77    pub fn verify(&self, expected_hash: &str) -> Result<(), ChecksumError> {
78        let expected = canonical_hash(expected_hash)?;
79        if self.hash == expected {
80            Ok(())
81        } else {
82            Err(ChecksumError::ChecksumMismatch {
83                expected,
84                actual: self.hash.clone(),
85            })
86        }
87    }
88}
89
90pub(crate) fn canonical_hash(hash: &str) -> Result<String, ChecksumError> {
91    let normalized = hash.to_ascii_lowercase();
92    normalized
93        .parse::<Sha256Digest>()
94        .map_err(|_| ChecksumError::InvalidHash(hash.to_owned()))?;
95    Ok(normalized)
96}
97
98/// Typed checksum-record validation failure.
99#[derive(Debug, Error)]
100pub enum ChecksumError {
101    /// The expected and observed exact bytes have different digests.
102    #[error("checksum mismatch: expected {expected}, actual {actual}")]
103    ChecksumMismatch {
104        /// Canonical expected digest.
105        expected: String,
106        /// Canonical observed digest.
107        actual: String,
108    },
109    /// A digest is not exactly 64 hexadecimal digits.
110    #[error("invalid SHA-256 checksum: {0}")]
111    InvalidHash(String),
112    /// Only the maintained SHA-256 algorithm is accepted.
113    #[error("unsupported checksum algorithm {0}")]
114    UnsupportedAlgorithm(String),
115}
116
117#[cfg(test)]
118mod tests;