libvault 0.2.2

the libvault is modified from RustyVault
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Miscellaneous public handy functions are collected here, such as cryptography tools,
//! uuid generator, etc.

use std::time::{Duration, SystemTime};

use blake3;
use chrono::prelude::*;
use humantime::{format_rfc3339, parse_duration, parse_rfc3339};
use openssl::hash::{Hasher, MessageDigest};
use rand::{Rng, rng};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashSet;

use crate::errors::RvError;

pub mod cert;
pub mod cidr;
pub mod crypto;
pub mod ip_sock_addr;
pub mod key;
pub mod kv_builder;
pub mod locks;
pub mod ocsp;
pub mod policy;
pub mod salt;
pub mod seal;
pub mod sock_addr;
pub mod string;
pub mod token_util;
pub mod unix_sock_addr;

/// A hash set that stores Blake3 hashes of arbitrary byte data.
///
/// BHashSet (Blake Hash Set) provides a space-efficient way to track whether
/// specific byte sequences have been "used" or seen before. Instead of storing
/// the actual data, it stores 32-byte Blake3 hashes, providing excellent
/// collision resistance while using constant space per item.
///
/// # Use Cases
/// - Tracking used unseal keys to prevent replay attacks
/// - Deduplication of data based on content
/// - Efficient membership testing for large byte sequences
/// - Preventing reuse of tokens, nonces, or other security-sensitive data
///
/// # Security Features
/// - Uses Blake3 cryptographic hash function for collision resistance
/// - Provides deterministic membership testing
/// - Space-efficient storage (32 bytes per unique item regardless of original size)
/// - Serializable for persistence across restarts
///
/// # Performance
/// - O(1) average case for membership testing and insertion
/// - Memory usage scales with number of unique items, not their size
/// - Blake3 hashing is extremely fast
///
/// # Example
/// ```
/// use libvault::utils::BHashSet;
///
/// let mut set = BHashSet::default();
///
/// // Insert some keys
/// set.insert(b"secret_key_1");
/// set.insert(b"secret_key_2");
///
/// // Check membership
/// assert!(set.contains(b"secret_key_1"));
/// assert!(!set.contains(b"unknown_key"));
///
/// // The set only stores hashes, not the original data
/// assert_eq!(set.len(), 2);
///
/// // Each stored hash is exactly 32 bytes regardless of input size
/// for hash in set.iter() {
///     assert_eq!(hash.len(), 32);
/// }
/// ```
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct BHashSet(pub HashSet<[u8; 32]>);

impl BHashSet {
    /// Checks if the set contains a specific key.
    ///
    /// This method computes the Blake3 hash of the provided key and checks
    /// if that hash exists in the internal hash set. This provides efficient
    /// membership testing without storing the original key data.
    ///
    /// # Arguments
    /// - `key`: The byte slice to check for membership
    ///
    /// # Returns
    /// `true` if the key's hash exists in the set, `false` otherwise
    ///
    /// # Performance
    /// - Time complexity: O(1) average case
    /// - Space complexity: O(1) - no additional memory allocated
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// set.insert(b"example_key");
    /// assert!(set.contains(b"example_key"));
    /// assert!(!set.contains(b"nonexistent_key"));
    /// ```
    pub fn contains(&self, key: &[u8]) -> bool {
        let hash: [u8; 32] = blake3::hash(key).into();
        self.0.contains(&hash)
    }

    /// Inserts a key into the set.
    ///
    /// This method computes the Blake3 hash of the provided key and stores
    /// the hash in the internal hash set. If the key was already present,
    /// this operation has no effect.
    ///
    /// # Arguments
    /// - `key`: The byte slice to insert into the set
    ///
    /// # Performance
    /// - Time complexity: O(1) average case
    /// - Space complexity: O(1) per unique key (32 bytes for the hash)
    ///
    /// # Security
    /// - Only stores cryptographic hash, not the original key data
    /// - Provides collision resistance through Blake3 hashing
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// set.insert(b"secure_token");
    /// assert!(set.contains(b"secure_token"));
    /// ```
    pub fn insert(&mut self, key: &[u8]) {
        let hash: [u8; 32] = blake3::hash(key).into();
        self.0.insert(hash);
    }

    /// Removes a key from the set.
    ///
    /// This method computes the Blake3 hash of the provided key and removes
    /// that hash from the internal hash set. If the key was not present,
    /// this operation has no effect.
    ///
    /// # Arguments
    /// - `key`: The byte slice to remove from the set
    ///
    /// # Returns
    /// `true` if the key was present and removed, `false` if it wasn't present
    ///
    /// # Performance
    /// - Time complexity: O(1) average case
    /// - Space complexity: O(1) - may free 32 bytes if key was present
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// set.insert(b"temporary_key");
    /// assert!(set.contains(b"temporary_key"));
    /// set.remove(b"temporary_key");
    /// assert!(!set.contains(b"temporary_key"));
    /// ```
    pub fn remove(&mut self, key: &[u8]) -> bool {
        let hash: [u8; 32] = blake3::hash(key).into();
        self.0.remove(&hash)
    }

    /// Clears all entries from the set.
    ///
    /// This method removes all stored hashes from the internal hash set,
    /// effectively resetting the set to an empty state. All previously
    /// inserted keys will no longer be considered as contained in the set.
    ///
    /// # Performance
    /// - Time complexity: O(n) where n is the number of stored hashes
    /// - Space complexity: Frees all allocated memory for stored hashes
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// set.insert(b"key1");
    /// set.insert(b"key2");
    /// assert_eq!(set.len(), 2);
    ///
    /// set.clear();
    /// assert_eq!(set.len(), 0);
    /// assert!(set.is_empty());
    /// ```
    pub fn clear(&mut self) {
        self.0.clear();
    }

    /// Returns the number of unique keys stored in the set.
    ///
    /// This method returns the count of unique Blake3 hashes stored
    /// in the internal hash set, which corresponds to the number of
    /// unique keys that have been inserted.
    ///
    /// # Returns
    /// The number of unique keys in the set
    ///
    /// # Performance
    /// - Time complexity: O(1)
    /// - Space complexity: O(1)
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// assert_eq!(set.len(), 0);
    ///
    /// set.insert(b"key1");
    /// set.insert(b"key2");
    /// set.insert(b"key1"); // Duplicate, won't increase count
    /// assert_eq!(set.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Checks if the set is empty.
    ///
    /// This method returns `true` if the set contains no keys,
    /// `false` otherwise. It's equivalent to checking if `len() == 0`
    /// but may be more efficient.
    ///
    /// # Returns
    /// `true` if the set is empty, `false` otherwise
    ///
    /// # Performance
    /// - Time complexity: O(1)
    /// - Space complexity: O(1)
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// assert!(set.is_empty());
    ///
    /// set.insert(b"key");
    /// assert!(!set.is_empty());
    ///
    /// set.clear();
    /// assert!(set.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns an iterator over the hashes stored in the set.
    ///
    /// This method provides an iterator that yields references to the
    /// Blake3 hashes stored in the set. Note that these are the computed
    /// hashes, not the original key data, as the original keys are not
    /// stored for security and space efficiency reasons.
    ///
    /// # Returns
    /// An iterator that yields `&[u8]` references to the 32-byte Blake3 hashes
    ///
    /// # Performance
    /// - Iterator creation: O(1)
    /// - Iteration: O(n) where n is the number of stored hashes
    ///
    /// # Security Note
    /// The returned hashes are cryptographically secure representations
    /// of the original keys, but the original key data cannot be recovered
    /// from these hashes.
    ///
    /// # Example
    /// ```
    /// use libvault::utils::BHashSet;
    ///
    /// let mut set = BHashSet::default();
    /// set.insert(b"key1");
    /// set.insert(b"key2");
    ///
    /// let hash_count = set.iter().count();
    /// assert_eq!(hash_count, 2);
    ///
    /// // Each hash is 32 bytes long (Blake3 output size)
    /// for hash in set.iter() {
    ///     assert_eq!(hash.len(), 32);
    /// }
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = &[u8]> {
        self.0.iter().map(|hash| hash.as_slice())
    }
}

pub fn generate_uuid() -> String {
    let mut buf = [0u8; 16];
    rng().fill(&mut buf);

    format!(
        "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
        buf[0],
        buf[1],
        buf[2],
        buf[3],
        buf[4],
        buf[5],
        buf[6],
        buf[7],
        buf[8],
        buf[9],
        buf[10],
        buf[11],
        buf[12],
        buf[13],
        buf[14],
        buf[15]
    )
}

pub fn is_str_subset<T: PartialEq>(sub: &Vec<T>, superset: &Vec<T>) -> bool {
    sub.iter().all(|item| superset.contains(item))
}

pub fn sha1(data: &[u8]) -> String {
    let mut hasher = Hasher::new(MessageDigest::sha1()).unwrap();
    hasher.update(data).unwrap();
    let result = hasher.finish().unwrap();
    hex::encode(result)
}

pub fn sha256(data: &[u8]) -> String {
    let mut hasher = Hasher::new(MessageDigest::sha256()).unwrap();
    hasher.update(data).unwrap();
    let result = hasher.finish().unwrap();
    hex::encode(result)
}

pub fn serialize_system_time<S>(time: &SystemTime, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let formatted = format_rfc3339(*time).to_string();
    serializer.serialize_str(&formatted)
}

pub fn deserialize_system_time<'de, D>(deserializer: D) -> Result<SystemTime, D::Error>
where
    D: Deserializer<'de>,
{
    let input: &str = Deserialize::deserialize(deserializer)?;
    let parsed_time = parse_rfc3339(input).map_err(serde::de::Error::custom)?;
    let system_time: SystemTime = parsed_time;
    Ok(system_time)
}

pub fn serialize_duration<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    let timestamp = duration.as_secs();
    serializer.serialize_i64(timestamp as i64)
}

pub fn deserialize_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
    D: serde::Deserializer<'de>,
{
    struct DurationVisitor;

    impl serde::de::Visitor<'_> for DurationVisitor {
        type Value = Duration;

        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("a number or a string with 's' suffix")
        }

        fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            Ok(Duration::from_secs(value))
        }

        fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
        where
            E: serde::de::Error,
        {
            parse_duration(value).map_err(serde::de::Error::custom)
        }
    }

    deserializer.deserialize_any(DurationVisitor)
}

pub fn asn1time_to_timestamp(time_str: &str) -> Result<i64, RvError> {
    // Parse the time string
    let dt = NaiveDateTime::parse_from_str(time_str, "%b %e %H:%M:%S %Y %Z")?;

    // Convert to a DateTime object with UTC timezone
    //let dt_utc = DateTime::<Utc>::from_utc(dt, Utc);
    let dt_utc = Utc.from_utc_datetime(&dt);

    // Get the timestamp
    let timestamp = dt_utc.timestamp();

    Ok(timestamp)
}

pub fn hex_encode_with_colon(bytes: &[u8]) -> String {
    let hex_str = hex::encode(bytes);
    let split_hex: Vec<String> = hex_str
        .as_bytes()
        .chunks(2)
        .map(|chunk| String::from_utf8(chunk.to_vec()).unwrap())
        .collect();

    split_hex.join(":")
}

pub fn is_protect_path(protected: &[&str], paths: &[&str]) -> bool {
    for p in protected.iter() {
        for path in paths.iter() {
            if path.starts_with(p) {
                return true;
            }
        }
    }

    false
}

pub fn default_system_time() -> SystemTime {
    SystemTime::UNIX_EPOCH
}