Crate encon

Source
Expand description

Encon is an optionally-encrypted config format, built on top of JSON. A mix of encrypted and plain fields, and support for encrypting arbitrary JSON values make it very flexible.

§Example

use serde_json::json;
use encon::{Password, Map, Encryptable};

let pass = Password::new("strongpassword");

let mut map = Map::new();
map.insert("foo", Encryptable::Plain("Foo".into()));
map.insert("bar", Encryptable::Plain("Bar".into()));
map.get_mut(&"foo".to_owned()).unwrap().intend_encrypted();

assert_eq!(map.get(&"foo".to_owned()).unwrap().is_encrypted(), false);
assert_eq!(map.get(&"bar".to_owned()).unwrap().is_encrypted(), false);

map.apply_all_intents(&pass).unwrap();
assert_eq!(map.get(&"foo".to_owned()).unwrap().is_encrypted(), true);
assert_eq!(map.get(&"bar".to_owned()).unwrap().is_encrypted(), false);

let json = map.to_json_pretty().unwrap();
let mut map2: Map = serde_json::from_str(&json).unwrap();
assert_eq!(map2.get(&"foo".to_owned()).unwrap().is_encrypted(), true);
assert_eq!(map2.get(&"bar".to_owned()).unwrap().is_encrypted(), false);

let value = map2.get_mut(&"foo".to_owned()).unwrap()
    .to_decrypted(&pass).unwrap()
    .as_plain().unwrap().clone();
assert_eq!(value, json!("Foo"));

Modules§

legacy

Structs§

Map
Represents a mapping of string keys to WithIntent values (a wrapper around Encryptable).
Password
Represents an encryption password, and contains the low level encrypt/decrypt operations on lists of bytes.
PlainMap
Similar to Map, but with all fields decrypted into Values.
WithIntent
A wrapper around Encryptable that also has an intent flag, indicating if we’d like it to be outputted as encrypted or plain.

Enums§

DecryptError
An error that may arise during decryption.
EnconError
High level error enum for when a single error type like DecryptError is too specific.
EncryptError
An error that may arise during encryption. Generally it’s expected that encryption doesn’t fail.
Encryptable
A value that can either be encrypted or plain, functionality to transition between the two states, and a pretty serde representation. In either variant, it represents an arbitrary JSON value.
EncryptableKind
Pairs with Encryptable, and is used in WithIntent.
MapToJsonError
An error arising from converting a Map to JSON (either pretty or compact)

Constants§

SIGNATURE

Functions§

init
init() initializes the sodium library and chooses faster versions of the primitives if possible. init() also makes the random number generation functions thread-safe
string
Shorthand for creating an Encryptable::Plain from text (not interpreted as JSON).