lib/
mod.rs

1#[allow(dead_code)]
2pub mod sha1_hash_map;
3use core::panic;
4use hex::encode;
5use sha1::{Digest, Sha1};
6use std::{
7    collections::{hash_map::DefaultHasher, HashMap},
8    hash::{self, Hash, Hasher},
9    sync::Arc,
10};
11
12#[derive(Debug, Clone, Default, PartialEq, Eq)]
13pub struct HashSetMap<T> {
14    pub register: HashMap<Arc<u64>, Arc<T>>,
15}
16
17pub struct HashSetMapBuilder<T> {
18    pub hash_register: HashSetMap<T>,
19    pub hasher: Option<Arc<dyn Hasher>>,
20}
21
22pub struct Sha1Hasher {
23    pub state: [u8; 20],
24}
25pub struct HsmWithSha1Hasher {
26    pub hsm: HashSetMapBuilder<String>,
27    pub hasher: Sha1Hasher,
28}
29
30#[allow(dead_code)]
31impl<'a, T> HashSetMapBuilder<T>
32where
33    T: Hash + Eq + Clone + Default + ToString,
34    HashSetMapBuilder<T>: Default,
35{
36    pub fn insert(mut self, hasher: &'a mut dyn Hasher, input: T) -> (u64, Option<Arc<T>>, Self) {
37        if self.hasher.is_none() {
38            panic!("HashSetMapBuilder::insert() -> hasher is None")
39        };
40        hasher.write(input.to_string().as_bytes());
41        let key = hasher.finish();
42        // print!("{}|", key);
43        (
44            key,
45            self.hash_register
46                .register
47                .insert(Arc::new(key), Arc::new(input)),
48            self,
49        )
50    }
51    pub fn new() -> Self {
52        Default::default()
53    }
54    pub fn new_with_capacity(capacity: usize) -> Self {
55        let mut hash_register = HashSetMap::default();
56        hash_register.register = HashMap::with_capacity(capacity);
57        Self {
58            hash_register,
59            hasher: None,
60        }
61    }
62    pub fn new_with_hasher(hasher: Arc<dyn Hasher>) -> Self {
63        Self {
64            hash_register: Default::default(),
65            hasher: Some(hasher),
66        }
67    }
68    pub fn new_with_capacity_and_hasher(capacity: usize, hasher: Arc<dyn Hasher>) -> Self {
69        let mut hash_register = HashSetMap::default();
70        hash_register.register = HashMap::with_capacity_and_hasher(capacity, Default::default());
71        Self {
72            hash_register,
73            hasher: Some(hasher),
74        }
75    }
76    pub fn build(self) -> HashMap<Arc<u64>, Arc<T>> {
77        self.hash_register.register
78    }
79
80    pub fn with_hasher(mut self, hasher: Arc<dyn Hasher>) -> Self {
81        self.hasher = Some(hasher);
82        self
83    }
84    pub fn hasher(&mut self, hasher: Arc<dyn Hasher>) -> &mut Self {
85        self.hasher = Some(hasher);
86        self
87    }
88
89    pub fn len(&self) -> usize {
90        self.hash_register.register.len()
91    }
92    pub fn is_empty(&self) -> bool {
93        self.hash_register.register.is_empty()
94    }
95    pub fn clear(&mut self) {
96        self.hash_register.register.clear();
97    }
98}
99
100impl<T> Default for HashSetMapBuilder<T>
101where
102    T: Hash + Eq + Clone + Default + ToString,
103{
104    fn default() -> Self {
105        Self {
106            hash_register: HashSetMap {
107                register: HashMap::new(),
108            },
109            hasher: Some(Arc::new(DefaultHasher::new())),
110        }
111    }
112}
113
114#[allow(dead_code)]
115impl Sha1Hasher
116where
117    Sha1Hasher: hash::Hasher,
118{
119    pub fn new() -> Self {
120        Self { state: [0; 20] }
121    }
122    fn digest(&self, data: &[u8]) -> String {
123        let mut hasher = Sha1Hasher::new();
124        hasher.write(data);
125        encode(hasher.state.as_ref())
126        // hasher.finish()
127    }
128    pub fn write(&mut self, bytes: &[u8]) {
129        // println!("{:?}", bytes);
130        let mut hasher = Sha1::new();
131        hasher.update(bytes);
132        let encode = encode(hasher.finalize()).as_bytes().to_vec();
133        // println!("{:#?}", encode);
134        self.state = encode[0..20].try_into().unwrap();
135    }
136
137    pub fn finish(&self) -> u64 {
138        let mut output: u64 = 0;
139        for &byte in self.state[0..7].iter() {
140            // print!("{}", byte);
141            output = output << 8;
142            output += byte as u64;
143        }
144        output
145    }
146}
147
148#[allow(dead_code)]
149impl hash::Hasher for Sha1Hasher {
150    fn finish(&self) -> u64 {
151        self.finish()
152    }
153    fn write(&mut self, bytes: &[u8]) {
154        self.write(bytes);
155    }
156}
157
158#[allow(dead_code)]
159impl HsmWithSha1Hasher {
160    pub fn new() -> Self {
161        Self {
162            hsm: HashSetMapBuilder::new(),
163            hasher: Sha1Hasher::new(),
164        }
165    }
166    pub fn insert(mut self, input: String) -> (u64, Option<Arc<String>>, Self) {
167        let key;
168        let previous;
169        (key, previous, self.hsm) = self.hsm.insert(&mut self.hasher, input);
170        (key, previous, self)
171    }
172    pub fn insert_blind(mut self, input: String) -> Self {
173        let _key;
174        let _previous;
175        (_key, _previous, self) = self.insert(input);
176        self
177    }
178    pub fn build(self) -> HashMap<Arc<u64>, Arc<String>> {
179        self.hsm.build()
180    }
181}