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
use std::hash::{Hash, Hasher};

use crate::traits::*;

/// Wrapper type to update hasher using the TL representation of type `T`.
#[derive(Eq)]
pub struct HashWrapper<T>(pub T);

impl<T> HashWrapper<T>
where
    T: TlWrite,
{
    /// Updates the specified hasher this the TL representation of inner data.
    #[inline(always)]
    pub fn update_hasher<H: digest::Update>(&self, engine: &mut H) {
        self.0.write_to(&mut DigestWriter(engine));
    }
}

impl<T: PartialEq> PartialEq for HashWrapper<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

struct DigestWriter<'a, T>(&'a mut T);

impl<T> TlPacket for DigestWriter<'_, T>
where
    T: digest::Update,
{
    const TARGET: TlTarget = TlTarget::Hasher;

    #[inline(always)]
    fn write_u32(&mut self, data: u32) {
        self.0.update(&data.to_le_bytes());
    }

    #[inline(always)]
    fn write_i32(&mut self, data: i32) {
        self.0.update(&data.to_le_bytes());
    }

    #[inline(always)]
    fn write_u64(&mut self, data: u64) {
        self.0.update(&data.to_le_bytes());
    }

    #[inline(always)]
    fn write_i64(&mut self, data: i64) {
        self.0.update(&data.to_le_bytes());
    }

    #[inline(always)]
    fn write_raw_slice(&mut self, data: &[u8]) {
        self.0.update(data);
    }
}

impl<T> Hash for HashWrapper<T>
where
    T: TlWrite,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.write_to(&mut HashWriter(state))
    }
}

struct HashWriter<'a>(pub &'a mut dyn Hasher);

impl<'a> TlPacket for HashWriter<'a> {
    const TARGET: TlTarget = TlTarget::Hasher;

    #[inline(always)]
    fn write_u32(&mut self, data: u32) {
        Hasher::write_u32(&mut self.0, data);
    }

    #[inline(always)]
    fn write_i32(&mut self, data: i32) {
        Hasher::write_i32(&mut self.0, data);
    }

    #[inline(always)]
    fn write_u64(&mut self, data: u64) {
        Hasher::write_u64(&mut self.0, data);
    }

    #[inline(always)]
    fn write_i64(&mut self, data: i64) {
        Hasher::write_i64(&mut self.0, data);
    }

    #[inline(always)]
    fn write_raw_slice(&mut self, data: &[u8]) {
        Hasher::write(&mut self.0, data);
    }
}