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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! A hasher that will be a wrapper over any  
//! [`std::io::Write`][std::io::Write] /  
//! [`futures::io::AsyncWrite`][futures::io::AsyncWrite] /  
//! [`tokio::io::AsyncWrite`][tokio::io::AsyncWrite] object  
//!
//!  You can wrap any of the previous trait object inside and that will transparently hash the data that is being
//!  written to it.  
//!
//!
//! The object should implement AsyncRead so that it can wrap some data and then read from that
//! transparently while offloading the hashing to another thread.
//! ```rust
//! extern crate sha2;
//! use write_hasher::{WriteHasher, MinDigest};
//! let mut src = std::fs::File::open(".gitignore").unwrap();
//! let sink = std::io::sink();
//! let mut hasher = WriteHasher::<sha2::Sha256, _>::new(sink);
//! std::io::copy(&mut src, &mut hasher).unwrap();
//! let x = hasher.finalize();
//! let x = format!("{:x}", x);
//! assert_eq!(
//!     "c1e953ee360e77de57f7b02f1b7880bd6a3dc22d1a69e953c2ac2c52cc52d247",
//!     x
//! );
//! ```

#[cfg(all(
    feature = "digest",
    any(
        feature = "concrete_impls",
        feature = "sha1",
        feature = "sha2",
        feature = "md2",
        feature = "md4",
        feature = "md5",
        feature = "blake2",
        feature = "crc32fast"
    )
))]
compile_error!("Please either use digest feature (for generic impls) or
               concrete_impls (sha1, sha2, md2, md4, md5, blake2, crc32fast) features (for concrete impls),
               but not both");

#[cfg(any(feature = "futures", feature = "tokio"))]
use core::pin::Pin;
use core::task::Poll;
#[cfg(feature = "digest")]
use digest::Digest;

/// A hasher that will be a wrapper over any Write / AsyncWrite object and transparently calculate
/// hash for any data written to it
#[cfg_attr(any(feature = "futures", feature = "tokio"), pin_project::pin_project)]
#[derive(Default)]
pub struct WriteHasher<D, T> {
    hasher: D,
    #[cfg_attr(any(feature = "futures", feature = "tokio"), pin)]
    inner: T,
}

impl<D, T> WriteHasher<D, T> {
    pub fn new_with_hasher(inner: T, hasher: D) -> Self {
        Self { hasher, inner }
    }

    pub fn new(inner: T) -> Self
    where
        D: Default,
    {
        Self {
            hasher: Default::default(),
            inner,
        }
    }
}

// #[cfg(feature = "digest")]
// impl<D: Digest, T> WriteHasher<D, T> {
//     pub fn new(inner: T) -> Self {
//         Self {
//             hasher: D::new(),
//             inner,
//         }
//     }
// }

#[cfg(feature = "digest")]
impl<D: Digest + digest::Reset, T> WriteHasher<D, T> {
    pub fn reset(&mut self) {
        <D as Digest>::reset(&mut self.hasher)
    }
}

/// A minimal version of [`Digest`][digest::digest] trait that is used to implement the WriteHasher
/// and all implementations of the Digest trait.
pub trait MinDigest {
    type Output;
    fn update(&mut self, data: impl AsRef<[u8]>);
    fn finalize(self) -> Self::Output;
}

impl<MD: MinDigest, T> MinDigest for WriteHasher<MD, T> {
    type Output = MD::Output;
    fn update(&mut self, data: impl AsRef<[u8]>) {
        self.hasher.update(data)
    }
    fn finalize(self) -> MD::Output {
        self.hasher.finalize()
    }
}

#[cfg(feature = "digest")]
impl<T: Digest> MinDigest for T {
    type Output = digest::Output<T>;
    fn update(&mut self, data: impl AsRef<[u8]>) {
        <T as Digest>::update(self, data)
    }
    fn finalize(self) -> Self::Output {
        <T as Digest>::finalize(self)
    }
}

#[cfg(any(
    feature = "sha2",
    feature = "sha1",
    feature = "md2",
    feature = "md4",
    feature = "md5",
    feature = "blake2"
))]
macro_rules! delegate_digest_mindigest {
    ($($x:ty),*) => {
        $(
            impl MinDigest for $x {
                type Output = digest::Output<$x>;
                fn update(&mut self, data: impl AsRef<[u8]>) {
                    <Self as digest::Digest>::update(self, data)
                }
                fn finalize(self) -> Self::Output {
                    <Self as digest::Digest>::finalize(self)
                }
            }

            impl<T> crate::WriteHasher<$x, T> {
                pub fn new(inner: T) -> Self {
                    Self {
                        hasher: <$x as ::digest::Digest>::new(),
                        inner,
                    }
                }
            }

        )*
    };
}

#[cfg(feature = "sha2")]
mod sha2 {
    use super::MinDigest;
    delegate_digest_mindigest!(
        sha2::Sha224,
        sha2::Sha256,
        sha2::Sha384,
        sha2::Sha512,
        sha2::Sha512_224,
        sha2::Sha512_256
    );
}

#[cfg(feature = "sha1")]
mod sha1 {
    use super::MinDigest;
    delegate_digest_mindigest!(sha1::Sha1);
}

#[cfg(feature = "md2")]
mod md2 {
    use super::MinDigest;
    delegate_digest_mindigest!(md2::Md2);
}

#[cfg(feature = "md4")]
mod md4 {
    use super::MinDigest;
    delegate_digest_mindigest!(md4::Md4);
}

#[cfg(feature = "md5")]
mod md5 {
    use super::MinDigest;
    impl MinDigest for md5::Context {
        type Output = md5::Digest;
        fn update(&mut self, data: impl AsRef<[u8]>) {
            self.consume(data)
        }
        fn finalize(self) -> Self::Output {
            self.compute()
        }
    }

    impl<T> crate::WriteHasher<md5::Context, T> {
        pub fn new(inner: T) -> Self {
            Self {
                hasher: md5::Context::new(),
                inner,
            }
        }
    }
}

#[cfg(feature = "blake2")]
mod blake2 {
    use super::MinDigest;
    // use digest::consts::*;
    // use digest::typenum::*;

    // delegate_digest_mindigest!(blake2::Blake2b);
    delegate_digest_mindigest!(blake2::Blake2b512);
    // delegate_digest_mindigest!(blake2::Blake2bCore);
    // delegate_digest_mindigest!(blake2::Blake2bMac512);
    // delegate_digest_mindigest!(blake2::Blake2bVar);
    // delegate_digest_mindigest!(blake2::Blake2s);
    delegate_digest_mindigest!(blake2::Blake2s256);
    // delegate_digest_mindigest!(blake2::Blake2sCore);
    // delegate_digest_mindigest!(blake2::Blake2sMac256);
    // delegate_digest_mindigest!(blake2::Blake2sVar);
}

#[cfg(feature = "crc32fast")]
mod crc32fast {
    use super::MinDigest;
    impl MinDigest for crc32fast::Hasher {
        type Output = u32;
        fn update(&mut self, data: impl AsRef<[u8]>) {
            self.update(data.as_ref())
        }
        fn finalize(self) -> Self::Output {
            self.finalize()
        }
    }

    impl<T> crate::WriteHasher<crc32fast::Hasher, T> {
        pub fn new(inner: T) -> Self {
            Self {
                hasher: crc32fast::Hasher::new(),
                inner,
            }
        }
    }
}

// #[cfg(feature = "crc32c")]
pub mod crc32c {
    use super::MinDigest;
    #[repr(transparent)]
    #[derive(Debug, Default)]
    pub struct Crc32c(u32);

    impl Crc32c {
        pub fn new() -> Self {
            Default::default()
        }
    }

    impl MinDigest for Crc32c {
        type Output = u32;
        fn update(&mut self, data: impl AsRef<[u8]>) {
            self.0 = crc32c::crc32c_append(self.0, data.as_ref())
        }
        fn finalize(self) -> Self::Output {
            self.0
        }
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
#[cfg(feature = "tokio")]
impl<D: MinDigest, T: tokio::io::AsyncWrite + std::marker::Unpin> tokio::io::AsyncWrite
    for WriteHasher<D, T>
{
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<std::io::Result<usize>> {
        let ah = self.project();
        let r = ah.inner.poll_write(cx, buf);
        if let Poll::Ready(Ok(n)) = r {
            ah.hasher.update(&buf[..n]);
        }
        r
    }
    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        let ah = self.project();
        ah.inner.poll_flush(cx)
    }
    fn poll_shutdown(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        let ah = self.project();
        ah.inner.poll_shutdown(cx)
    }
}

#[cfg(feature = "futures")]
impl<D: MinDigest, T: futures::io::AsyncWrite + std::marker::Unpin> futures::io::AsyncWrite
    for WriteHasher<D, T>
{
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<futures::io::Result<usize>> {
        let ah = self.project();
        let r = ah.inner.poll_write(cx, buf);
        if let Poll::Ready(Ok(n)) = r {
            ah.hasher.update(&buf[..n]);
        }
        r
    }
    fn poll_flush(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<futures::io::Result<()>> {
        let ah = self.project();
        ah.inner.poll_flush(cx)
    }
    fn poll_close(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<futures::io::Result<()>> {
        let ah = self.project();
        ah.inner.poll_close(cx)
    }
}

#[cfg(feature = "stdio")]
impl<D: MinDigest, T: std::io::Write> std::io::Write for WriteHasher<D, T> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let r = std::io::Write::write(&mut self.inner, buf);
        if let Ok(n) = r {
            MinDigest::update(&mut self.hasher, &buf[..n]);
        }
        r
    }
    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[tokio::test]
    #[cfg(feature = "tokio")]
    #[cfg(any(feature = "sha2", feature = "digest"))]
    async fn test_read() {
        extern crate sha2;
        let mut src = tokio::fs::File::open(".gitignore").await.unwrap();
        let sink = tokio::io::sink();
        let mut hasher = WriteHasher::<sha2::Sha256, _>::new(sink);
        tokio::io::copy(&mut src, &mut hasher).await.unwrap();
        // hasher.write_all(b"hello worlding").await.unwrap();
        let x = hasher.finalize();
        let x = format!("{:x}", x);
        assert_eq!(
            "c1e953ee360e77de57f7b02f1b7880bd6a3dc22d1a69e953c2ac2c52cc52d247",
            x
        );
    }

    #[tokio::test]
    #[cfg(feature = "futures")]
    #[cfg(any(feature = "sha2", feature = "digest"))]
    async fn test_read_futures() {
        extern crate sha2;
        let src = std::fs::read(".gitignore").unwrap();
        let src = futures::io::Cursor::new(src);
        let sink = futures::io::sink();
        let mut hasher = WriteHasher::<sha2::Sha256, _>::new(sink);
        futures::io::copy(src, &mut hasher).await.unwrap();
        // hasher.write_all(b"hello worlding").await.unwrap();
        let x = hasher.finalize();
        let x = format!("{:x}", x);
        assert_eq!(
            "c1e953ee360e77de57f7b02f1b7880bd6a3dc22d1a69e953c2ac2c52cc52d247",
            x
        );
    }

    #[tokio::test]
    #[cfg(feature = "tokio")]
    #[cfg(feature = "crc32fast")]
    async fn test_crc32() {
        extern crate crc32fast;
        let mut src = tokio::fs::File::open(".gitignore").await.unwrap();
        let sink = tokio::io::sink();
        let mut hasher =
            WriteHasher::<crc32fast::Hasher, _>::new_with_hasher(sink, Default::default());
        tokio::io::copy(&mut src, &mut hasher).await.unwrap();
        // hasher.write_all(b"hello worlding").await.unwrap();
        let x = hasher.finalize();
        assert_eq!(x, 0x705ffe14);
    }

    #[test]
    #[cfg(feature = "stdio")]
    #[cfg(any(feature = "sha2", feature = "digest"))]
    fn test_read_stdio() {
        extern crate sha2;
        let mut src = std::fs::File::open(".gitignore").unwrap();
        let sink = std::io::sink();
        let mut hasher = WriteHasher::<sha2::Sha256, _>::new(sink);
        std::io::copy(&mut src, &mut hasher).unwrap();
        // hasher.write_all(b"hello worlding").await.unwrap();
        let x = hasher.finalize();
        let x = format!("{:x}", x);
        assert_eq!(
            "c1e953ee360e77de57f7b02f1b7880bd6a3dc22d1a69e953c2ac2c52cc52d247",
            x
        );
    }

    #[tokio::test]
    #[ignore]
    #[cfg(all(feature = "tokio", feature = "stdio"))]
    #[cfg(any(feature = "crc32c", feature = "digest"))]
    async fn test_tokio_bigfile() {
        let mut src = tokio::fs::File::open("file.zip").await.unwrap();
        let sink = tokio::io::sink();
        let mut hasher = WriteHasher::<crc32c::Crc32c, _>::new(sink);
        tokio::io::copy(&mut src, &mut hasher).await.unwrap();
        // hasher.write_all(b"hello worlding").await.unwrap();
        let x = hasher.finalize();
        assert_eq!(x, 0xbd7a7dfe);
        let mut src = std::fs::File::open("file.zip").unwrap();
        let sink = std::io::sink();
        let mut hasher = WriteHasher::<crc32c::Crc32c, _>::new(sink);
        std::io::copy(&mut src, &mut hasher).unwrap();
        // hasher.write_all(b"hello worlding").await.unwrap();
        let y = hasher.finalize();
        assert_eq!(x, y);
        assert_eq!(3178921470, x);
        assert_eq!(3178921470, y);
    }
}