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
/// Cryptographic and encoding utilities.
///
/// This module provides high-performance hashing, encoding/decoding,
/// compression/decompression, and byte manipulation utilities using
/// optimized algorithms and data structures.
///
/// # Examples
///
/// Basic hashing and encoding:
/// ```rust
/// use trash_utilities::common::crypto::*;
///
/// // Fast hashing
/// let hash = fast_hash(b"hello world");
/// assert_ne!(hash, 0);
///
/// // Base64 encoding/decoding
/// let data = b"secret data";
/// let encoded = encode_base64(data);
/// let decoded = decode_base64(&encoded).unwrap();
/// assert_eq!(decoded, data);
///
/// // Compression
/// let compressed = compress_brotli(b"some data to compress", 6).unwrap();
/// let decompressed = decompress_brotli(&compressed).unwrap();
/// assert_eq!(decompressed, b"some data to compress");
/// ```
// Standard library imports
use ;
// External crate imports
use AHashMap;
use ;
use ;
use Bytes;
/// Creates a new empty `AHashMap` with the `AHash` hashing algorithm.
///
/// `AHash` is a high-performance hashing algorithm, often faster than the standard
/// library's hasher for hash maps.
///
/// # Type Parameters
/// - `K`: The key type, must implement `Hash` and `Eq`.
/// - `V`: The value type.
///
/// # Returns
/// An empty `AHashMap<K, V>`.
///
/// # Examples
/// ```rust
/// use trash_analyzer::common::crypto::create_ahash_map;
/// use ahash::AHashMap;
///
/// let mut map: AHashMap<String, i32> = create_ahash_map();
/// map.insert("key".to_string(), 42);
/// ```
/// Wraps a byte slice into a `Bytes` object for efficient sharing.
///
/// This function creates a copy of the data in `Bytes`, which is useful for
/// zero-copy operations in async contexts.
///
/// # Parameters
/// - `data`: The byte slice to wrap.
///
/// # Returns
/// A `Bytes` object containing a copy of the input data.
///
/// # Examples
/// ```rust
/// use trash_analyzer::common::crypto::wrap_bytes;
/// use bytes::Bytes;
///
/// let data = b"Hello, world!";
/// let bytes = wrap_bytes(data);
/// assert_eq!(bytes.as_ref(), data);
/// ```
/// Encodes binary data to a Base64 string.
///
/// This function uses the standard Base64 encoding without padding.
///
/// # Parameters
/// - `data`: The byte slice to encode.
///
/// # Returns
/// A `String` containing the Base64-encoded data.
///
/// # Examples
/// ```rust
/// use trash_analyzer::common::crypto::encode_base64;
///
/// let data = b"Hello, world!";
/// let encoded = encode_base64(data);
/// println!("Encoded: {}", encoded);
/// ```
/// Decodes a Base64 string to binary data.
///
/// This function decodes standard Base64 without padding.
///
/// # Parameters
/// - `data`: The Base64-encoded string to decode.
///
/// # Returns
/// - `Ok(Vec<u8>)` containing the decoded bytes if successful.
/// - `Err(base64::DecodeError)` if decoding fails (e.g., invalid Base64).
///
/// # Errors
///
/// Returns a `base64::DecodeError` if the input is not valid base64.
///
/// # Examples
/// ```rust
/// use trash_analyzer::common::crypto::{encode_base64, decode_base64};
///
/// let data = b"Hello, world!";
/// let encoded = encode_base64(data);
/// let decoded = decode_base64(&encoded).unwrap();
/// assert_eq!(decoded, data);
/// ```
/// Compress data using Brotli
///
/// Uses the Brotli compression algorithm for efficient data compression.
/// Higher compression levels provide better compression but are slower.
///
/// # Parameters
///
/// * `data` - The byte data to compress.
/// * `level` - Compression level (1-11, where 1 is fastest, 11 is best compression).
///
/// # Returns
///
/// The compressed data as a byte vector on success.
///
/// # Errors
///
/// Returns an `std::io::Error` if compression fails.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::crypto::compress_brotli;
///
/// let data = b"This is some data to compress";
/// let compressed = compress_brotli(data, 6).unwrap(); // Level 6 is a good balance
/// assert!(compressed.len() < data.len()); // Compressed data should be smaller
/// ```
/// Decompress Brotli data
///
/// Decompresses data that was compressed with Brotli.
///
/// # Parameters
///
/// * `data` - The compressed Brotli data to decompress.
///
/// # Returns
///
/// The decompressed data as a byte vector on success.
///
/// # Errors
///
/// Returns an `std::io::Error` if decompression fails (e.g., corrupted data).
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::crypto::{compress_brotli, decompress_brotli};
///
/// let original = b"Hello, compressed world!";
/// let compressed = compress_brotli(original, 6).unwrap();
/// let decompressed = decompress_brotli(&compressed).unwrap();
/// assert_eq!(decompressed, original);
/// ```
/// Compute fast hash of data using ahash
///
/// Uses the `AHash` algorithm for high-performance hashing.
/// `AHash` is designed to be fast while maintaining good distribution properties.
///
/// # Parameters
///
/// * `data` - The byte data to hash.
///
/// # Returns
///
/// A 64-bit hash value.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::crypto::fast_hash;
///
/// let hash1 = fast_hash(b"hello");
/// let hash2 = fast_hash(b"world");
/// assert_ne!(hash1, hash2);
/// assert_eq!(fast_hash(b"hello"), hash1); // Deterministic
/// ```
/// Compute fast hash of data with key using ahash
///
/// Computes a hash that incorporates both a key and data.
/// Useful for keyed hashing operations where you want different keys to produce different hashes.
///
/// # Parameters
///
/// * `key` - The key bytes to include in the hash.
/// * `data` - The data bytes to hash.
///
/// # Returns
///
/// A 64-bit hash value combining both key and data.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::crypto::keyed_hash;
///
/// let key = b"secret_key";
/// let data = b"message";
/// let hash = keyed_hash(key, data);
/// // Same key+data always produces same hash
/// assert_eq!(keyed_hash(key, data), hash);
/// // Different key produces different hash
/// assert_ne!(keyed_hash(b"other_key", data), hash);
/// ```
/// Verify keyed hash
///
/// Verifies that the provided data with the given key produces the expected hash.
/// Useful for integrity checking and authentication.
///
/// # Parameters
///
/// * `key` - The key bytes used in the original hash.
/// * `data` - The data bytes to verify.
/// * `expected` - The expected hash value.
///
/// # Returns
///
/// `true` if the computed hash matches the expected value, `false` otherwise.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::common::crypto::{keyed_hash, verify_keyed_hash};
///
/// let key = b"my_key";
/// let data = b"important data";
/// let hash = keyed_hash(key, data);
///
/// // Verification should succeed
/// assert!(verify_keyed_hash(key, data, hash));
///
/// // Verification should fail with wrong hash
/// assert!(!verify_keyed_hash(key, data, hash + 1));
///
/// // Verification should fail with wrong key
/// assert!(!verify_keyed_hash(b"wrong_key", data, hash));
/// ```