use crate::aead;
use crate::encoding as enc;
use crate::token;
pub use crate::types::{Alg, Encoding, MANIFEST_KEY};
pub fn seal(octets: &[u8], key: &[u8; 64], alg: Alg) -> Option<Vec<u8>> {
aead::seal(octets, key, alg).ok()
}
pub fn open(sealed: &[u8], key: &[u8; 64], alg: Alg) -> Option<Vec<u8>> {
aead::open(sealed, key, alg)
}
pub fn encode(bytes: &[u8], encoding: Encoding) -> String {
let mut out = String::new();
enc::encode_into(bytes, encoding, &mut out);
out
}
pub fn decode(text: &str, encoding: Encoding) -> Option<Vec<u8>> {
enc::decode(text, encoding)
}
#[derive(Clone, Debug)]
pub struct Half {
pub alg: char,
pub text: String,
}
#[derive(Clone, Debug)]
pub struct Parsed {
pub encoding: Encoding,
pub separator: char,
pub manifest: Option<Half>,
pub mandate: Option<Half>,
pub mandate_part: String,
}
pub fn parse(token: &str) -> Option<Parsed> {
let p = token::parse(token).ok()?;
let half = |h: token::Half<'_>| Half {
alg: h.alg_code,
text: h.text.to_string(),
};
Some(Parsed {
encoding: p.encoding,
separator: p.separator,
manifest: p.manifest.map(half),
mandate: p.mandate.map(half),
mandate_part: p.mandate_part.to_string(),
})
}