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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! Compute cryptographic hash digests of content, in a way where you'd
//! have to go very explicitly out of your way to fake a hash or get it
//! wrong.
//!
//! ```
//! use arkive::Digest;
//!
//! let d: Digest = "foo".into();
//! println!("{}", d.to_hex());
//! ```

// Don't pollute namespace, just make sure we're loading some traits
use hex::ToHex;
use serde::de;
use serde::de::Visitor;
use sha2::Digest as _;

/// Trait for upstream vendor tools which produce digests.
pub trait Hasher<const N: usize>: sha2::Digest + Sized {
    /// Produce an N-byte digest as a raw byte array.
    fn into_bytes(self) -> [u8; N];

    /// Produce a Dirtabase digest type.
    fn into_digest(self) -> D<N> {
        D::<N>(self.into_bytes())
    }
}

// Need one of these per algorithm
impl Hasher<{ 256 / 8 }> for sha2::Sha256 {
    fn into_bytes(self) -> [u8; 256 / 8] {
        self.finalize().as_slice().try_into().unwrap()
    }
}
impl Hasher<{ 512 / 8 }> for sha2::Sha512 {
    fn into_bytes(self) -> [u8; 512 / 8] {
        self.finalize().as_slice().try_into().unwrap()
    }
}

/// Generic digest implementation.
///
/// This is used to make the "real" types you'd use every day, particularly
/// Digest. This flexibility should be somewhat helpful if Sha256 ever proves
/// inadequate, which isn't likely in the _near_ future, but is plausible on a
/// long enough timescale.
#[derive(PartialEq, Copy, Clone)]
pub struct D<const N: usize>([u8; N]);
impl<const N: usize> D<N> {
    /// Machine-friendly borrow of digest bytes.
    pub fn to_bytes(&self) -> &[u8; N] {
        &self.0
    }

    /// Import from some external source. Be careful to preserve invariants!
    pub fn from_bytes(bytes: &[u8; N]) -> Self {
        Self(bytes.clone())
    }

    /// Human-friendly representation of digest bytes.
    pub fn to_hex(&self) -> String {
        self.to_bytes().encode_hex()
    }

    /// Create a digest from a text hex
    pub fn from_hex(h: impl AsRef<[u8]>) -> Result<Self, hex::FromHexError> {
        let mut bytes = [0u8; N];
        hex::decode_to_slice(h, &mut bytes)?;
        Ok(Self(bytes))
    }
}

impl<const N: usize> std::fmt::Debug for D<N> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Digest({:?})", self.to_hex())
    }
}
impl<const N: usize> serde::Serialize for D<N> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_hex())
    }
}
impl<'de, const N: usize> serde::Deserialize<'de> for D<N> {
    fn deserialize<DE>(deserializer: DE) -> Result<Self, DE::Error>
    where
        DE: serde::Deserializer<'de>,
    {
        struct HexVisitor<const N: usize>;
        impl<'de, const N: usize> Visitor<'de> for HexVisitor<N> {
            type Value = D<N>;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "a hex string representing {} bytes", N)
            }

            fn visit_str<E>(self, value: &str) -> Result<D<N>, E>
            where
                E: de::Error,
            {
                let vec = hex::decode(value).map_err(de::Error::custom)?;
                let bytes: [u8; N] = vec
                    .try_into()
                    .map_err(|o: Vec<u8>| de::Error::invalid_length(o.len(), &self))?;
                Ok(D::from_bytes(&bytes))
            }
        }

        deserializer.deserialize_str(HexVisitor::<N>)
    }
}

// We divide by 8 since these are named after the number of bits, not bytes.
pub type DigestSha256 = D<{ 256 / 8 }>;
pub type DigestSha512 = D<{ 512 / 8 }>;

/// The default Digest type used throughout Dirtabase.
pub type Digest = DigestSha256;

impl<T> From<T> for DigestSha256
where
    T: AsRef<[u8]>,
{
    fn from(data: T) -> Self {
        sha2::Sha256::new().chain_update(data).into_digest()
    }
}
impl<T> From<T> for DigestSha512
where
    T: AsRef<[u8]>,
{
    fn from(data: T) -> Self {
        sha2::Sha512::new().chain_update(data).into_digest()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn simple_demo() {
        let hasher = sha2::Sha256::new();
        let d = hasher.chain_update("Hello world!").into_digest();
        assert_eq!(
            d.to_hex(),
            "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a"
        );
    }

    #[test]
    fn partial_eq() {
        let d1: Digest = "Hello from D1!".into();
        let d2: Digest = "Hello from D2!".into();
        assert_eq!(d1, d1);
        assert!(d1 != d2);
    }

    #[test]
    fn into() {
        let d: Digest = "Hello world!".into();
        assert_eq!(
            d.to_hex(),
            "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a"
        );
    }

    #[test]
    fn serialize() {
        let d: Digest = "Hello world!".into();
        let s = serde_json::to_string(&d).expect("failed to serialize");
        assert_eq!(
            s,
            "\"c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a\""
        );
    }
    #[test]
    fn deserialize() {
        let s = "\"c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a\"";
        let d: Digest = serde_json::from_str(&s).expect("failed to deserialize");
        assert_eq!(d, Digest::from("Hello world!"));

        // Error scenario 1: not a JSON string
        let d = serde_json::from_str::<Digest>("123");
        assert!(d.is_err());

        // Error scenario 2: it's not hex
        let d = serde_json::from_str::<Digest>("\"text but not hex\"");
        assert!(d.is_err());

        // Error scenario 3: Wrong length
        let d = serde_json::from_str::<Digest>("\"123\"");
        assert!(d.is_err());
    }

    #[test]
    fn from_sha256() {
        let d = Digest::from("Hello world!");
        assert_eq!(
            &d.to_hex(),
            "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a"
        );
        assert_eq!(d.to_bytes()[0..3], [192, 83, 94]);
    }

    #[test]
    fn from_sha512() {
        let d = DigestSha512::from("Hello world!");
        assert_eq!(
            &d.to_hex(),
            "f6cde2a0f819314cdde55fc227d8d7dae3d28cc556222a0a8ad66d91ccad4aad6094f517a2182360c9aacf6a3dc323162cb6fd8cdffedb0fe038f55e85ffb5b6"
        );
        assert_eq!(d.to_bytes()[0..3], [246, 205, 226]);
    }
}