pub mod aead;
pub mod chacha;
pub mod kdf;
pub mod poly1305;
#[derive(Clone)]
pub struct SecretKey([u8; 32]);
impl SecretKey {
pub fn new(bytes: [u8; 32]) -> SecretKey {
SecretKey(bytes)
}
pub fn bytes(&self) -> &[u8; 32] {
&self.0
}
}
impl Drop for SecretKey {
fn drop(&mut self) {
self.0 = [0u8; 32];
std::hint::black_box(&self.0);
}
}
impl std::fmt::Debug for SecretKey {
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
out.write_str("SecretKey(..)")
}
}
pub fn hex_bytes(text: &str) -> Vec<u8> {
let digits: Vec<u8> = text
.bytes()
.filter(|byte| !byte.is_ascii_whitespace())
.collect();
digits
.chunks_exact(2)
.map_while(|pair| {
let high = (*pair.first()? as char).to_digit(16)?;
let low = (*pair.get(1)? as char).to_digit(16)?;
u8::try_from(high.checked_mul(16)?.checked_add(low)?).ok()
})
.collect()
}
pub fn from_hex<const N: usize>(text: &str) -> [u8; N] {
let mut out = [0u8; N];
for (slot, byte) in out.iter_mut().zip(hex_bytes(text)) {
*slot = byte;
}
out
}