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
#![allow(dead_code)]
use data_encoding::BASE64_NOPAD;
use ring::digest;
use std::collections::HashMap;
use std::fmt;
use std::sync::RwLock;
lazy_static!{
static ref LOCAL_STORE: LocalStore = LocalStore::new();
}
pub(crate) fn get_global() -> &'static LocalStore {
&*LOCAL_STORE
}
#[derive(Debug)]
pub struct LocalStore {
store: RwLock<HashMap<String, Vec<u8>>>,
}
pub trait Store: fmt::Debug + Send + Sync {
fn insert(&self, key: &[u8]) -> String;
fn get_key(&self, id: &str) -> Option<Vec<u8>>;
}
impl LocalStore {
fn new() -> Self {
Self { store: RwLock::new(HashMap::new()) }
}
}
impl Store for LocalStore {
fn insert(&self, key: &[u8]) -> String {
let digest = digest::digest(&digest::SHA512_256, key);
let key_id = BASE64_NOPAD.encode(digest.as_ref());
let _ = self.store
.write()
.expect("could not get write on key store")
.insert(key_id.clone(), key.to_vec());
key_id
}
fn get_key(&self, id: &str) -> Option<Vec<u8>> {
if let Some(v) = self.store.read().expect("could not get read lock on key store").get(id) {
Some(v.clone())
} else {
None
}
}
}