diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-04-09 11:27:14 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-04-16 09:04:28 +0300 |
commit | d9b72ffa5485f3c253da22f09ff0a7090de7aa37 (patch) | |
tree | ffcfc0d0b2377c95072b609095589fbf83424382 /src/checksummer.rs | |
parent | 9f6ff22ff9a1b0a5a28d037846b1cecee4f2945c (diff) | |
download | obnam2-d9b72ffa5485f3c253da22f09ff0a7090de7aa37.tar.gz |
refactor: rename Checksum to Label
Label is a clearer and more accurate name for the type now that it is
not just a checksum.
Also, serialize a Label in tests, rather than using string literals.
This is more correct, and we'll be changing serialization later.
Sponsored-by: author
Diffstat (limited to 'src/checksummer.rs')
-rw-r--r-- | src/checksummer.rs | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/src/checksummer.rs b/src/checksummer.rs deleted file mode 100644 index 6d7303b..0000000 --- a/src/checksummer.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! Compute checksums of data. -//! -//! 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 Checksum { - /// An arbitrary, literal string. - Literal(String), - - /// A SHA256 checksum. - Sha256(String), -} - -impl Checksum { - /// 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 Checksum { - /// 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), - } - } -} |