diff options
Diffstat (limited to 'src/label.rs')
-rw-r--r-- | src/label.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/label.rs b/src/label.rs new file mode 100644 index 0000000..7ee55d1 --- /dev/null +++ b/src/label.rs @@ -0,0 +1,49 @@ +//! A chunk label. +//! +//! De-duplication of backed up data in Obnam relies on cryptographic +//! checksums. They are implemented in this module. Note that Obnam +//! does not aim to make these algorithms configurable, so only a very +//! small number of carefully chosen algorithms are supported here. + +use sha2::{Digest, Sha256}; +use std::fmt; + +/// A checksum of some data. +#[derive(Debug, Clone)] +pub enum Label { + /// An arbitrary, literal string. + Literal(String), + + /// A SHA256 checksum. + Sha256(String), +} + +impl Label { + /// Construct a literal string. + pub fn literal(s: &str) -> Self { + Self::Literal(s.to_string()) + } + + /// Compute a SHA256 checksum for a block of data. + pub fn sha256(data: &[u8]) -> Self { + let mut hasher = Sha256::new(); + hasher.update(data); + let hash = hasher.finalize(); + Self::Sha256(format!("{:x}", hash)) + } + + /// Create a `Checksum` from a known, previously computed hash. + pub fn sha256_from_str_unchecked(hash: &str) -> Self { + Self::Sha256(hash.to_string()) + } +} + +impl fmt::Display for Label { + /// Format a checksum for display. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Literal(s) => write!(f, "{}", s), + Self::Sha256(hash) => write!(f, "{}", hash), + } + } +} |