summaryrefslogtreecommitdiff
path: root/src/label.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-04-09 12:00:29 +0300
committerLars Wirzenius <liw@liw.fi>2022-04-16 09:06:59 +0300
commit18c0f4afab29e17c050208234becbfb5e2973746 (patch)
tree62bb67504c47747f8ce202f4eb4121bb3d051223 /src/label.rs
parent82ff782fe85c84c10f1f18c9bd5c2b017bc2f240 (diff)
downloadobnam2-18c0f4afab29e17c050208234becbfb5e2973746.tar.gz
feat: use one checksum for all chunks in a backup
When making a backup, use the same checksum for any chunks it re-uses or creates. This is for performance: if we allowed two checksums to be used, we would have to compute the checksum for a chunk twice, and potentially look up both on the server. This is just a lot of work. Instead, we use only one. The trade-off here is that when (not if) the user wants to switch to a new checksum type, they'll have to do a full backup, uploading all their data to the server, even when it's already there, just with a different checksum. Hopefully this will be rare. Full backups always use the built-in, hardcoded default checksum, and incremental backups use whatever the previous backup used. The default is still SHA256, but this commit add code to support BLAKE2 if we decide to switch that as a default. It's also easy to add support for others, now. BLAKE2 was added to verify that Obnam can actually handle the checksum changing (manual test: not in the test suite). I don't think users need to be offered even the option of choosing a checksum algorithm to use. When one cares about both security and performance, choosing a checksum requires specialist, expert knowledge. Obnam developers should choose the default. Giving users a knob they can twiddle just makes it that much harder to configure and use Obnam. If the choice Obnam developers have made is shown to be sub-optimal, it seems better to change the default for everyone, rather than hope that every user changes their configuration to gain the benefit. Experience has shown that people mostly don't change the default configuration, and that they are especially bad at choosing well when security is a concern. (Obnam is free software. Expert users can choose their checksum by changing the source code. I'm not fundamentally limiting anyone's freedom or choice here.) Users can switch to a new default algorithm by triggering a full backup with the new "obnam backup --full". Sponsored-by: author
Diffstat (limited to 'src/label.rs')
-rw-r--r--src/label.rs54
1 files changed, 53 insertions, 1 deletions
diff --git a/src/label.rs b/src/label.rs
index 64be341..19d270a 100644
--- a/src/label.rs
+++ b/src/label.rs
@@ -5,10 +5,12 @@
//! does not aim to make these algorithms configurable, so only a very
//! small number of carefully chosen algorithms are supported here.
+use blake2::Blake2s256;
use sha2::{Digest, Sha256};
const LITERAL: char = '0';
const SHA256: char = '1';
+const BLAKE2: char = '2';
/// A checksum of some data.
#[derive(Debug, Clone)]
@@ -18,6 +20,9 @@ pub enum Label {
/// A SHA256 checksum.
Sha256(String),
+
+ /// A BLAKE2s checksum.
+ Blake2(String),
}
impl Label {
@@ -34,11 +39,20 @@ impl Label {
Self::Sha256(format!("{:x}", hash))
}
+ /// Compute a BLAKE2s checksum for a block of data.
+ pub fn blake2(data: &[u8]) -> Self {
+ let mut hasher = Blake2s256::new();
+ hasher.update(data);
+ let hash = hasher.finalize();
+ Self::Sha256(format!("{:x}", hash))
+ }
+
/// Serialize a label into a string representation.
pub fn serialize(&self) -> String {
match self {
Self::Literal(s) => format!("{}{}", LITERAL, s),
Self::Sha256(hash) => format!("{}{}", SHA256, hash),
+ Self::Blake2(hash) => format!("{}{}", BLAKE2, hash),
}
}
@@ -54,6 +68,37 @@ impl Label {
}
}
+/// Kinds of checksum labels.
+#[derive(Debug, Clone, Copy, Eq, PartialEq)]
+pub enum LabelChecksumKind {
+ /// Use a Blake2 checksum.
+ Blake2,
+
+ /// Use a SHA256 checksum.
+ Sha256,
+}
+
+impl LabelChecksumKind {
+ /// Parse a string into a label checksum kind.
+ pub fn from(s: &str) -> Result<Self, LabelError> {
+ if s == "sha256" {
+ Ok(Self::Sha256)
+ } else if s == "blake2" {
+ Ok(Self::Blake2)
+ } else {
+ Err(LabelError::UnknownType(s.to_string()))
+ }
+ }
+
+ /// Serialize a checksum kind into a string.
+ pub fn serialize(self) -> &'static str {
+ match self {
+ Self::Sha256 => "sha256",
+ Self::Blake2 => "blake2",
+ }
+ }
+}
+
/// Possible errors from dealing with chunk labels.
#[derive(Debug, thiserror::Error)]
pub enum LabelError {
@@ -64,7 +109,7 @@ pub enum LabelError {
#[cfg(test)]
mod test {
- use super::Label;
+ use super::{Label, LabelChecksumKind};
#[test]
fn roundtrip_literal() {
@@ -83,4 +128,11 @@ mod test {
let seri2 = de.serialize();
assert_eq!(serialized, seri2);
}
+
+ #[test]
+ fn roundtrip_checksum_kind() {
+ for kind in [LabelChecksumKind::Sha256, LabelChecksumKind::Blake2] {
+ assert_eq!(LabelChecksumKind::from(kind.serialize()).unwrap(), kind);
+ }
+ }
}