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
//! Message digest algorithm

use md5::Md5;
use ring::digest::{Context, SHA1, SHA1_OUTPUT_LEN};

use bytes::BufMut;

/// Digest trait
pub trait Digest: Send {
    /// Update data
    fn update(&mut self, data: &[u8]);

    /// Generates digest
    fn digest<B: BufMut>(&mut self, buf: &mut B);

    /// Length of digest
    fn digest_len(&self) -> usize;

    /// Reset digest
    fn reset(&mut self);
}

/// Type of defined digests
#[derive(Clone, Copy)]
pub enum DigestType {
    Md5,
    Sha1,
    Sha,
}

/// Create digest with type
pub fn with_type(t: DigestType) -> DigestVariant {
    match t {
        DigestType::Md5 => DigestVariant::Md5(Md5::default()),
        DigestType::Sha1 | DigestType::Sha => DigestVariant::Sha1(Context::new(&SHA1)),
    }
}

/// Variant of supported digest
pub enum DigestVariant {
    Md5(Md5),
    Sha1(Context),
}

impl Digest for DigestVariant {
    fn update(&mut self, data: &[u8]) {
        use md5::Digest;

        match *self {
            DigestVariant::Md5(ref mut d) => d.input(data),
            DigestVariant::Sha1(ref mut d) => d.update(data),
        }
    }

    fn digest<B: BufMut>(&mut self, buf: &mut B) {
        use md5::Digest;

        match *self {
            DigestVariant::Md5(ref d) => buf.put(&*d.clone().result()),
            DigestVariant::Sha1(ref d) => buf.put(d.clone().finish().as_ref()),
        }
    }

    fn digest_len(&self) -> usize {
        use digest::FixedOutput;
        use typenum::Unsigned;

        match *self {
            DigestVariant::Md5(_) => <Md5 as FixedOutput>::OutputSize::to_usize(),
            DigestVariant::Sha1(_) => SHA1_OUTPUT_LEN,
        }
    }

    fn reset(&mut self) {
        match *self {
            DigestVariant::Md5(ref mut d) => d.clone_from(&Md5::default()),
            DigestVariant::Sha1(ref mut d) => d.clone_from(&Context::new(&SHA1)),
        }
    }
}