1个不稳定版本
0.1.0 | 2023年9月18日 |
---|
#8 in #encrypting
18KB
132 代码行
Serialize_with_password
这是一个小的库,它在上面的serde库之上添加了额外的层。该库允许使用密码加密序列化数据
示例
带密码的序列化
use serialize_with_password::{serialize, is_encrypted, deserialize};
let example_data = b"some data";
let password = b"password";
let encrypted = serialize(example_data, password).unwrap();
assert_ne!(example_data.to_vec(), encrypted);
assert!(is_encrypted(&encrypted).expect("Correctly encrypted data always will return Ok(bool) for is_encrypted"));
assert_eq!(example_data.to_vec(), deserialize(&encrypted, password).expect("Correct password"));
assert!(deserialize(&encrypted, b"bacPass").is_err());
不带密码的序列化
use serialize_with_password::{serialize_no_pass, is_encrypted, deserialize, deserialize_no_pass};
let example_data = b"some data";
let encoded = serialize_no_pass(example_data);
assert!(!is_encrypted(&encoded).unwrap());
assert_eq!(example_data.to_vec(), deserialize_no_pass(&encoded).unwrap());
assert_eq!(example_data.to_vec(), deserialize(&encoded, b"Any password").unwrap());
bbbb# 示例
带密码的serde序列化
use serde::{Serialize, Deserialize};
use serialize_with_password::{serialize_serde, is_encrypted, deserialize_serde};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct ExampleStruct {
data: i32,
more_data: Vec<u8>,
}
let example_data = ExampleStruct{ data: 16, more_data: vec![5,2,41,2] };
let password = b"password";
let encrypted = serialize_serde(&example_data, password).unwrap();
assert!(is_encrypted(&encrypted).unwrap());
assert_eq!(example_data, deserialize_serde(&encrypted, password).unwrap());
assert!(deserialize_serde::<ExampleStruct>(&encrypted, b"bacPass").is_err());
不带密码的serde序列化
use serde::{Serialize, Deserialize};
use serialize_with_password::{serialize_serde_no_pass, is_encrypted, deserialize_serde, deserialize_serde_no_pass};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct ExampleStruct {
data: i32,
more_data: Vec<u8>,
}
let example_data = ExampleStruct{ data: 16, more_data: vec![5,2,41,2] };
let encoded = serialize_serde_no_pass(&example_data).unwrap();
assert!(!is_encrypted(&encoded).unwrap());
assert_eq!(example_data, deserialize_serde_no_pass(&encoded).unwrap());
assert_eq!(example_data, deserialize_serde(&encoded, b"asdhas").unwrap());
依赖关系
~1–1.4MB
~26K SLoC