lighthouse-pulse/crypto/eth2_key_derivation/src/secret_bytes.rs
Paul Hauner 4331834003
Directory Restructure (#1163)
* Move tests -> testing

* Directory restructure

* Update Cargo.toml during restructure

* Update Makefile during restructure

* Fix arbitrary path
2020-05-18 21:24:23 +10:00

30 lines
690 B
Rust

use zeroize::Zeroize;
/// Provides a wrapper around a `Vec<u8>` that implements `Zeroize` on `Drop`.
#[derive(Zeroize)]
#[zeroize(drop)]
pub struct SecretBytes(Vec<u8>);
impl SecretBytes {
/// Instantiates `Self` with an all-zeros byte array of length `len`.
pub fn zero(len: usize) -> Self {
Self(vec![0; len])
}
/// Returns a reference to the underlying bytes.
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
/// Returns a mutable reference to the underlying bytes.
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl From<Vec<u8>> for SecretBytes {
fn from(vec: Vec<u8>) -> Self {
Self(vec)
}
}