enigma-storage 0.0.1

Encrypted local storage for Enigma with mandatory at-rest encryption and cross-platform key vault providers.
Documentation
use crate::error::{EnigmaStorageError, Result};

pub const ENVELOPE_VERSION: u8 = 1;

pub fn encode_envelope(nonce: &[u8; 24], ciphertext: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(1 + nonce.len() + ciphertext.len());
    out.push(ENVELOPE_VERSION);
    out.extend_from_slice(nonce);
    out.extend_from_slice(ciphertext);
    out
}

pub fn decode_envelope(data: &[u8]) -> Result<(u8, [u8; 24], Vec<u8>)> {
    if data.len() < 1 + 24 {
        return Err(EnigmaStorageError::CorruptedData);
    }
    let version = data[0];
    let mut nonce = [0u8; 24];
    nonce.copy_from_slice(&data[1..25]);
    let ciphertext = data[25..].to_vec();
    Ok((version, nonce, ciphertext))
}