#serialization #password #data #encryption #deserialize #serde #is-encrypted

serialize-with-password

Crate for serializing and encrypting data

1 unstable release

0.1.0 Sep 18, 2023

#310 in #password

MIT license

18KB
132 lines

Serialize_with_password

Small crate that adds additional layer on top of the serde crate. This crate allows to encrypt serialized data with password

Examples

Serialization with password

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());

Serialize without password

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# Examples

Serialize serde with password

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());

Serialize serde without password

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());

Dependencies

~1.5MB
~27K SLoC