shelter_storage/
lib.rs

1#![forbid(unsafe_code)]
2#[macro_use]
3extern crate serde_derive;
4extern crate bincode;
5extern crate moka;
6extern crate orion;
7extern crate serde;
8extern crate serde_bytes;
9
10mod cipher;
11mod filesystem;
12mod memory;
13mod super_block;
14mod vio;
15mod xchacha;
16
17pub use cipher::Cipher;
18pub use filesystem::FileSystem;
19pub use memory::MemoryStorage;
20use orion::aead::SecretKey;
21use std::sync::{Arc, RwLock};
22use super_block::SuperBlock;
23pub use xchacha::XChaCha;
24
25pub trait Storage: Send + Sync {
26    // Check if storage is init
27    fn is_init(&self) -> bool;
28
29    // Init the new storage and store payload
30    fn init(&mut self, password: &[u8], payload: &[u8]);
31
32    // open an existing storage and return payload
33    fn open(&mut self, password: &[u8]) -> Vec<u8>;
34
35    // make connection to storage
36    fn connect(&mut self);
37
38    // save payload into the super block
39    fn save_payload(&mut self, payload: &[u8]);
40
41    // block read/write, can be buffered
42    // storage doesn't need to gurantee update is persistent
43    fn get_block(&self, cid: &str) -> Vec<u8>;
44    fn put_block(&mut self, cid: &str, data: &[u8]);
45    fn del_block(&mut self, cid: &str);
46
47    fn is_exist(&self, cid: &str) -> bool;
48
49    // flush blocks
50    // storage must gurantee write is persistent
51    fn flush(&mut self);
52
53    // permanently destroy this storage
54    fn destroy(&mut self);
55}
56
57pub type StorageLock<S> = Arc<RwLock<S>>;
58
59// TODO[epic=feat]: generic Cipher
60pub trait Crypto: Send + Sync + serde::Serialize {
61    fn hash_password(&self, password: &[u8], salt: &[u8]) -> SecretKey;
62
63    fn get_cipher() -> Cipher;
64
65    fn encrypt_with_key(&self, key: &SecretKey, data: &[u8]) -> Vec<u8>;
66
67    fn decrypt_with_key(&self, key: &SecretKey, ciphertext: &[u8]) -> Vec<u8>;
68}
69
70struct CryptoUtil {}
71
72impl CryptoUtil {
73    pub fn gen_secret_key() -> SecretKey {
74        SecretKey::default()
75    }
76}
77
78/// Dummy storage
79#[derive(Debug, Default)]
80pub struct DummyStorage;
81
82impl Storage for DummyStorage {
83    fn is_init(&self) -> bool {
84        unimplemented!()
85    }
86
87    #[inline]
88    fn init(&mut self, _password: &[u8], _payload: &[u8]) {
89        unimplemented!()
90    }
91
92    #[inline]
93    fn open(&mut self, _password: &[u8]) -> Vec<u8> {
94        unimplemented!()
95    }
96
97    #[inline]
98    fn connect(&mut self) {
99        unimplemented!()
100    }
101
102    #[inline]
103    fn save_payload(&mut self, _payload: &[u8]) {
104        unimplemented!()
105    }
106
107    #[inline]
108    fn get_block(&self, _cid: &str) -> Vec<u8> {
109        unimplemented!()
110    }
111
112    #[inline]
113    fn put_block(&mut self, _cid: &str, _data: &[u8]) {
114        unimplemented!()
115    }
116
117    #[inline]
118    fn del_block(&mut self, _cid: &str) {
119        unimplemented!()
120    }
121
122    #[inline]
123    fn is_exist(&self, _cid: &str) -> bool {
124        unimplemented!()
125    }
126
127    #[inline]
128    fn flush(&mut self) {
129        unimplemented!()
130    }
131
132    #[inline]
133    fn destroy(&mut self) {
134        unimplemented!()
135    }
136}