use std::fmt;
use std::ops::{Deref, DerefMut};
#[derive(Clone, PartialEq)]
pub struct SecureVec(Vec<u8>);
impl AsRef<[u8]> for SecureVec {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl Deref for SecureVec {
type Target = Vec<u8>;
fn deref(&self) -> &Vec<u8> {
&self.0
}
}
impl DerefMut for SecureVec {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<Vec<u8>> for SecureVec {
fn from(inner: Vec<u8>) -> Self {
SecureVec(inner)
}
}
impl Drop for SecureVec {
fn drop(&mut self) {
self.0.resize(self.0.capacity(), 0);
for elem in self.0.iter_mut() {
*elem = 0;
}
}
}
impl fmt::Debug for SecureVec {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut dbg = fmt.debug_tuple("SecureVec");
if cfg!(feature = "debug-plain-keys") {
dbg.field(&format!("<{} bytes>", self.0.len())).finish()
} else {
dbg.field(&self.0).finish()
}
}
}