summaryrefslogtreecommitdiff
path: root/src/label.rs
blob: 19d270aca0a6e5a9a4f800a1c6c71be0ecaa6731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! 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 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)]
pub enum Label {
    /// An arbitrary, literal string.
    Literal(String),

    /// A SHA256 checksum.
    Sha256(String),

    /// A BLAKE2s checksum.
    Blake2(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))
    }

    /// 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),
        }
    }

    /// De-serialize a label from its string representation.
    pub fn deserialize(s: &str) -> Result<Self, LabelError> {
        if s.starts_with(LITERAL) {
            Ok(Self::Literal(s[1..].to_string()))
        } else if s.starts_with(SHA256) {
            Ok(Self::Sha256(s[1..].to_string()))
        } else {
            Err(LabelError::UnknownType(s.to_string()))
        }
    }
}

/// 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 {
    /// Serialized label didn't start with a known type prefix.
    #[error("Unknown label: {0:?}")]
    UnknownType(String),
}

#[cfg(test)]
mod test {
    use super::{Label, LabelChecksumKind};

    #[test]
    fn roundtrip_literal() {
        let label = Label::literal("dummy data");
        let serialized = label.serialize();
        let de = Label::deserialize(&serialized).unwrap();
        let seri2 = de.serialize();
        assert_eq!(serialized, seri2);
    }

    #[test]
    fn roundtrip_sha256() {
        let label = Label::sha256(b"dummy data");
        let serialized = label.serialize();
        let de = Label::deserialize(&serialized).unwrap();
        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);
        }
    }
}