Crate serde2file
source ·Expand description
serialize some data to a file or deserialize a data from a file by support encrypt/decrypt file
将struct序列化化存储为文件,或者从文件反序列化为struct 文件存储时支持加密存储
derive macro FileSerializeAndDeserialize Attributes(Optional):
- file_encrypt
- encrypt Data encryption method
- decrypt Data decryption method
- The above encryption and decryption methods must be set at the same time, otherwise encryption and decryption will not be performed.
- #[file_encrypt(encrypt=“TestEncryptTool::encrypt”,decrypt=“TestEncryptTool::decrypt”)]
- dump_file_name
- Custom dump file name
- If not set ,the default dump file name is the full name of the current Struct.
- For example, the default dump file name corresponding to serde2file::test::TestData is serde2file-test-TestData
- #[dump_file_name = “test_data”]
派生宏FileSerializeAndDeserialize属性(可选)
- file_encrypt
- encrypt 数据加密方法
- decrypt 数据解密方法
- 以上加密和解密方法必须同时设置,否则不进行加解密。
- #[file_encrypt(encrypt=“TestEncryptTool::encrypt”,decrypt=“TestEncryptTool::decrypt”)]
- dump_file_name
- 设置自定义的转储文件名称
- 自定义转储文件名称
- 默认为当前Struct的完整名称,
- 如serde2file::test::TestData对应的缺省转储文件名称为serde2file-test-TestData
- #[dump_file_name = “test_data”]
示例 / Example
ⓘ
use serde::{Deserialize, Serialize};
use aes_gcm::aead::{Aead, NewAead};
use aes_gcm::{Aes256Gcm, Key, Nonce};
use serde2file::prelude::*;
#[derive(Debug, Serialize, Deserialize, Default, PartialEq, Eq, FileSerializeAndDeserialize)]
#[file_encrypt(encrypt="TestEncryptTool::encrypt",decrypt="TestEncryptTool::decrypt")]
#[dump_file_name = "test_data"]
struct TestData {
id: String,
name: String,
}
/// a encryption and decryption tools
///
/// 自定义加密和解密工具
#[allow(dead_code)]
struct TestEncryptTool;
/// some
impl TestEncryptTool {
pub fn get_aes_key_nonce() -> (String, String) {
(
"*)_#@140600!$=_^20230208)leb*$xz".into(),
"abc$hy%95599".into(),
)
}
pub fn encrypt(data: &str) -> DumpResult<Vec<u8>> {
let (key_str, nonce_str) = Self::get_aes_key_nonce();
let key = Key::from_slice(key_str.as_bytes());
let cipher = Aes256Gcm::new(key);
let nonce = Nonce::from_slice(nonce_str.as_bytes());
let cipher_data = cipher.encrypt(nonce, data.as_bytes().as_ref()).unwrap();
Ok(cipher_data)
}
pub fn decrypt(data: Vec<u8>) -> DumpResult<Vec<u8>> {
let (key_str, nonce_str) = Self::get_aes_key_nonce();
let key = Key::from_slice(key_str.as_bytes());
let cipher = Aes256Gcm::new(key);
let nonce = Nonce::from_slice(nonce_str.as_bytes());
let plain_data = cipher.decrypt(nonce, data.as_ref()).unwrap();
Ok(plain_data)
}
}
#cfg(test)
mod test {
use super::*;
/// Test serialization and encrypted storage to file
/// 测试序列化并加密存储到文件
#[test]
fn test_serialize_to_file() {
let data = TestData {
id: "01".into(),
name: "hy".into(),
};
data.dump_to_file("/tmp").unwrap();
}
/// Test deserialization from encrypted file
/// 测试从加密文件反序列化
#[test]
fn test_deserialize_from_file() {
let data = TestData {
id: "01".into(),
name: "hy".into(),
};
let s_data = TestData::load_from_file("/tmp").unwrap();
assert_eq!(data, s_data)
}
}
Modules
Enums
Traits
A data structure that can be loaded and deserialized from a file
A data structure that can be serialized and dumped to a file
Type Definitions
Decryption method type
serialization or deserialization error
Encryption method type