Crate struct_box

Source
Expand description

Make any serializable struct or enum encryptable.

Usage is entirely straightforward, and is best demonstrated with an example:

use strong_box::{generate_key, StaticStrongBox, StrongBox};
use struct_box::StructBox;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, StructBox)]
struct SensitiveData {
    data: String,
}

// This is an example of how to setup a StrongBox, which is the encryption engine we use behind
// the scenes
let strong_key = generate_key();
let strong_box = StaticStrongBox::new(strong_key, vec![strong_key]);

// This is the data we want to securely encrypt
let data = SensitiveData { data: "something very important and secret".to_string() };

// The contents of ciphertext is a Vec<u8> that can be safely shared, stored, etc, without
// worrying about anyone who doesn't have the key being able to read it
let ciphertext = data.encrypt(&strong_box, b"encryption context")?;

// Of course, it's not much use if we can't *decrypt* it again, though...
let decrypted_data = SensitiveData::decrypt(&ciphertext, &strong_box, b"encryption context")?;

assert_eq!(data.data, decrypted_data.data);

As this derive macro is little more than a wrapper around strong-box, consult that crate’s documentation for details about available StrongBox types, and the importance of the “encryption context” that is passed into the encrypt and decrypt calls.

Derive Macros§

StructBox