#![cfg(feature = "encryption")]
mod aead;
mod kdf;
pub use aead::{NONCE_LEN, SALT_LEN, TAG_LEN, decrypt_body, encrypt_body, fill_random};
pub use kdf::{KEY_LEN, Key, derive_key_from_password};
use alloc::string::{String, ToString};
use core::fmt;
use crate::{Error, Result};
#[derive(Clone)]
pub enum EncryptionKey {
Raw(Key),
Password(String),
}
impl fmt::Debug for EncryptionKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Raw(_) => f.write_str("EncryptionKey::Raw(***)"),
Self::Password(_) => f.write_str("EncryptionKey::Password(***)"),
}
}
}
impl EncryptionKey {
pub fn parse_cli(s: &str) -> Result<Self> {
let t = s.trim();
if t.len() == 64 && t.chars().all(|c| c.is_ascii_hexdigit()) {
let mut key = [0u8; KEY_LEN];
for i in 0..KEY_LEN {
key[i] = u8::from_str_radix(&t[i * 2..i * 2 + 2], 16)
.map_err(|e| Error::InvalidParameter(format!("bad hex key: {e}")))?;
}
Ok(Self::Raw(key))
} else if !t.is_empty() {
Ok(Self::Password(t.to_string()))
} else {
Err(Error::InvalidParameter("empty encryption key".into()))
}
}
}