lthash_rs/
lib.rs

1mod lthash16;
2mod lthash32;
3mod utils;
4
5pub use lthash16::*;
6pub use lthash32::*;
7
8use digest::ExtendableOutput;
9
10/// Generic trait for LtHash, these functions will be implemented by all the instances of LtHash.
11pub trait LtHash {
12    /// Inserts an element to LtHash, actually it generates the hash (of size 2048 bytes) of the object and sums it to the checksum.
13    fn insert(&mut self, element: impl AsRef<[u8]>);
14    /// Removes an element to LtHash, actually it generates the hash (of size 2048 bytes) of the object and removes it from the checksum.
15    fn remove(&mut self, element: impl AsRef<[u8]>);
16    /// Provides the hex value as String of the checksum.
17    fn to_hex_string(&self) -> String;
18    /// Takes the union of `self` and `rhs`.
19    ///
20    /// Equivalent to cloning `self`, then adding all the objects in `rhs`.
21    ///
22    /// Equivalent to `self | other`.
23    fn union(&self, rhs: &Self) -> Self;
24    /// Takes the difference of `self` and `rhs`.
25    ///
26    /// Equivalent to cloning `self`, then removing all the objects in `rhs`.
27    ///
28    /// Equivalent to `self - other`.
29    fn difference(&self, rhs: &Self) -> Self;
30    /// Clears the internal checksum.
31    fn reset(&mut self);
32    /// Converts self into the inner list of bytes.
33    fn into_bytes(self) -> Vec<u8>;
34}
35
36macro_rules! common {
37    ($lthash:ty) => {
38        impl<A, H> Extend<A> for $lthash
39        where
40            A: AsRef<[u8]>,
41            H: ExtendableOutput + Default,
42        {
43            fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
44                for item in iter {
45                    self.insert(item);
46                }
47            }
48        }
49
50        impl<H> PartialEq for $lthash {
51            fn eq(&self, other: &Self) -> bool {
52                subtle::ConstantTimeEq::ct_eq(
53                    &self.checksum[..],
54                    &other.checksum[..],
55                )
56                .into()
57            }
58        }
59
60        impl<H> core::fmt::Debug for $lthash {
61            fn fmt(
62                &self,
63                f: &mut core::fmt::Formatter<'_>,
64            ) -> core::fmt::Result {
65                write!(f, "{} {:?}", self.name(), &self.checksum)
66            }
67        }
68
69        impl<A, H> FromIterator<A> for $lthash
70        where
71            A: AsRef<[u8]>,
72            H: ExtendableOutput + Default,
73        {
74            fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
75                let mut this = Self::default();
76                this.extend(iter);
77                this
78            }
79        }
80
81        impl<'a, H> std::ops::BitOr for &'a $lthash
82        where
83            H: ExtendableOutput + Default,
84        {
85            type Output = $lthash;
86
87            fn bitor(self, rhs: Self) -> Self::Output {
88                self.union(rhs)
89            }
90        }
91
92        impl<H> std::ops::BitOr for $lthash
93        where
94            H: ExtendableOutput + Default,
95        {
96            type Output = Self;
97
98            fn bitor(self, rhs: Self) -> Self::Output {
99                self.union(&rhs)
100            }
101        }
102
103        impl<'a, H> std::ops::Sub for &'a $lthash
104        where
105            H: ExtendableOutput + Default,
106        {
107            type Output = $lthash;
108
109            fn sub(self, rhs: Self) -> Self::Output {
110                self.difference(rhs)
111            }
112        }
113
114        impl<H> std::ops::Sub for $lthash
115        where
116            H: ExtendableOutput + Default,
117        {
118            type Output = Self;
119
120            fn sub(self, rhs: Self) -> Self::Output {
121                self.difference(&rhs)
122            }
123        }
124    };
125}
126
127common!(LtHash16<H>);
128common!(LtHash32<H>);