lighthouse/crypto/bls/src/plain_text.rs
Paul Hauner c93f9c351b
Improve bls::SecretKey privacy (#1164)
* Improve bls::SecretKey privacy

* Add missed file

* Remove more methods from bls::SecretKey

* Add as_bytes() to SecretKey, remove as_raw

* Remove as_raw

* Add back as_raw

* Address review comments
2020-05-19 11:23:08 +10:00

41 lines
842 B
Rust

use zeroize::Zeroize;
/// Provides wrapper around `Vec<u8>` that implements `Zeroize`.
#[derive(Zeroize, Clone, PartialEq)]
#[zeroize(drop)]
pub struct PlainText(Vec<u8>);
impl PlainText {
/// Instantiate self with `len` zeros.
pub fn zero(len: usize) -> Self {
Self(vec![0; len])
}
/// The byte-length of `self`
pub fn len(&self) -> usize {
self.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 PlainText {
fn from(vec: Vec<u8>) -> Self {
Self(vec)
}
}
impl AsRef<[u8]> for PlainText {
fn as_ref(&self) -> &[u8] {
&self.0
}
}