|
|
|
@ -82,7 +82,7 @@ impl Header for KeyHeader
|
|
|
|
|
fn hash(&self) -> Sha256Hash
|
|
|
|
|
{
|
|
|
|
|
let mut output = Vec::new();
|
|
|
|
|
self.write_bytes(&mut output).now_or_never().unwrap().expect("Failed to write bytes to in-memory buffer");
|
|
|
|
|
self.write_bytes(&mut output, None).now_or_never().unwrap().expect("Failed to write bytes to in-memory buffer");
|
|
|
|
|
crypto::sha256::compute_slice(output)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -136,7 +136,7 @@ impl KeyHeader
|
|
|
|
|
|
|
|
|
|
/// Write this superheader as text bytes to this stream
|
|
|
|
|
#[instrument(err, skip(out))]
|
|
|
|
|
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report>
|
|
|
|
|
pub async fn write_text<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, passwd: Option<&Password>) -> Result<usize, eyre::Report>
|
|
|
|
|
{
|
|
|
|
|
let text = serialise::into_text(self)
|
|
|
|
|
.wrap_err_with(|| eyre::eyre!("Failed to serialise header to text"))
|
|
|
|
@ -157,8 +157,8 @@ impl KeyHeader
|
|
|
|
|
Ok(written + 4)
|
|
|
|
|
}
|
|
|
|
|
/// Read a superheader as text bytes from this stream
|
|
|
|
|
#[instrument(err, skip(input))]
|
|
|
|
|
pub async fn read_text<T: AsyncBufRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report>
|
|
|
|
|
#[instrument(err, skip(input, passwd))]
|
|
|
|
|
pub async fn read_text<T: AsyncBufRead+Unpin+?Sized, F: FnOnce(&Salt) -> Option<Password>>(input: &mut T, passwd: F) -> Result<Self, eyre::Report>
|
|
|
|
|
{
|
|
|
|
|
let (mut tx, mut rx) = mpsc::channel(1);
|
|
|
|
|
|
|
|
|
@ -302,7 +302,7 @@ impl KeyHeader
|
|
|
|
|
}
|
|
|
|
|
/// Write this key header as bytes to this stream
|
|
|
|
|
#[instrument(err, skip(out))]
|
|
|
|
|
pub async fn write_bytes<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T) -> Result<usize, eyre::Report>
|
|
|
|
|
pub async fn write_bytes<T: AsyncWrite+Unpin+?Sized>(&self, out: &mut T, passwd: Option<&Password>) -> Result<usize, eyre::Report>
|
|
|
|
|
{
|
|
|
|
|
out.write_u8(self.kind as u8).await?;
|
|
|
|
|
let desc = {
|
|
|
|
@ -321,8 +321,8 @@ impl KeyHeader
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Read a key header as bytes from this stream
|
|
|
|
|
#[instrument(err, skip(input))]
|
|
|
|
|
pub async fn read_bytes<T: AsyncRead+Unpin+?Sized>(input: &mut T) -> Result<Self, eyre::Report>
|
|
|
|
|
#[instrument(err, skip(input, passwd))]
|
|
|
|
|
pub async fn read_bytes<T: AsyncRead+Unpin+?Sized, F: FnOnce(&Salt) -> Option<Password>>(input: &mut T, passwd: F) -> Result<Self, eyre::Report>
|
|
|
|
|
{
|
|
|
|
|
let kind = {
|
|
|
|
|
let byte = input.read_u8().await?;
|
|
|
|
@ -361,27 +361,27 @@ impl KeyHeader
|
|
|
|
|
|
|
|
|
|
impl serialise::BinarySerialisable for KeyHeader
|
|
|
|
|
{
|
|
|
|
|
#[inline(always)] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
|
|
|
|
|
#[inline(always)] fn serialise_bytes<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
|
|
|
|
|
{
|
|
|
|
|
self.write_bytes(out).boxed_local()
|
|
|
|
|
self.write_bytes(out, passwd).boxed_local()
|
|
|
|
|
}
|
|
|
|
|
#[inline(always)] fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized>(input: &'b mut T) -> LocalBoxFuture<'b, Result<Self, eyre::Report>>
|
|
|
|
|
#[inline(always)] fn deserialise_bytes<'b , T: AsyncRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password>+ 'b>(input: &'b mut T, passwd: F) -> LocalBoxFuture<'b, Result<Self, eyre::Report>>
|
|
|
|
|
where Self: 'b
|
|
|
|
|
{
|
|
|
|
|
Self::read_bytes(input).boxed_local()
|
|
|
|
|
Self::read_bytes(input, passwd).boxed_local()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl serialise::TextSerialiseable for KeyHeader
|
|
|
|
|
{
|
|
|
|
|
#[inline(always)] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
|
|
|
|
|
#[inline(always)] fn serialise_text<'a, 'b: 'a, T: AsyncWrite+Unpin+?Sized>(&'a self, out: &'b mut T, passwd: Option<&'b Password>) -> LocalBoxFuture<'a, Result<usize, eyre::Report>>
|
|
|
|
|
{
|
|
|
|
|
self.write_text(out).boxed_local()
|
|
|
|
|
self.write_text(out, passwd).boxed_local()
|
|
|
|
|
}
|
|
|
|
|
#[inline(always)] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized>(input: &'a mut T) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
|
|
|
|
|
#[inline(always)] fn deserialise_text<'a, T: AsyncBufRead+Unpin+?Sized, F: for<'r> FnOnce(&'r Salt) -> Option<Password> + 'a>(input: &'a mut T, passwd: F) -> LocalBoxFuture<'a, Result<Self, eyre::Report>>
|
|
|
|
|
where Self: 'a
|
|
|
|
|
{
|
|
|
|
|
Self::read_text(input).boxed_local()
|
|
|
|
|
Self::read_text(input, passwd).boxed_local()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -414,17 +414,18 @@ mod tests
|
|
|
|
|
|
|
|
|
|
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
|
|
|
|
|
let mut ser = Vec::new();
|
|
|
|
|
let password = Password::derive("hello world", &Default::default());
|
|
|
|
|
let superheader = SuperHeader::<KeyHeader>::new_for(&header);
|
|
|
|
|
println!("Writing: {:?} + {:?}", superheader, header);
|
|
|
|
|
let written = superheader.serialise_bytes(&mut ser).await? +
|
|
|
|
|
header.write_bytes(&mut ser).await?;
|
|
|
|
|
let written = superheader.serialise_bytes(&mut ser, Some(&password)).await? +
|
|
|
|
|
header.write_bytes(&mut ser,Some(&password)).await?;
|
|
|
|
|
println!("Wrote {} bytes", written);
|
|
|
|
|
println!("{}\n", ser.fmt_view());
|
|
|
|
|
|
|
|
|
|
let mut read = &ser[..];
|
|
|
|
|
let reads = SuperHeader::deserialise_bytes(&mut read).await?;
|
|
|
|
|
let reads = SuperHeader::deserialise_bytes(&mut read, |_| Some(password.clone())).await?;
|
|
|
|
|
println!("Read super: {:?}", reads);
|
|
|
|
|
let readheader = KeyHeader::deserialise_bytes(&mut read).await?;
|
|
|
|
|
let readheader = KeyHeader::deserialise_bytes(&mut read, |_| Some(password.clone())).await?;
|
|
|
|
|
println!("Read real: {:?}", readheader);
|
|
|
|
|
|
|
|
|
|
reads.verify_for(&header)?;
|
|
|
|
@ -440,11 +441,11 @@ mod tests
|
|
|
|
|
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
|
|
|
|
|
let mut ser = Vec::new();
|
|
|
|
|
println!("Writing {:?}", header);
|
|
|
|
|
let val = header.write_bytes(&mut ser).await?;
|
|
|
|
|
let val = header.write_bytes(&mut ser, None).await?;
|
|
|
|
|
println!("Wrote {} bytes:", val);
|
|
|
|
|
println!("{}\n", ser.fmt_view());
|
|
|
|
|
|
|
|
|
|
let reader = KeyHeader::read_bytes(&mut &ser[..]).await?;
|
|
|
|
|
let reader = KeyHeader::read_bytes(&mut &ser[..], |_| None).await?;
|
|
|
|
|
println!("Read: {:?}", reader);
|
|
|
|
|
assert_eq!(reader, header);
|
|
|
|
|
|
|
|
|
@ -457,11 +458,11 @@ mod tests
|
|
|
|
|
let header = KeyHeader::new_now(KeyHeaderKind::Aes, Default::default(), Default::default());
|
|
|
|
|
let mut ser = Vec::new();
|
|
|
|
|
println!("Writing {:?}", header);
|
|
|
|
|
let val = header.write_text(&mut ser).await?;
|
|
|
|
|
let val = header.write_text(&mut ser, None).await?;
|
|
|
|
|
println!("Wrote {} bytes:", val);
|
|
|
|
|
println!("{}\n", ser.fmt_view());
|
|
|
|
|
|
|
|
|
|
let reader = KeyHeader::read_text(&mut &ser[..]).await?;
|
|
|
|
|
let reader = KeyHeader::read_text(&mut &ser[..], |_| None).await?;
|
|
|
|
|
println!("Read: {:?}", reader);
|
|
|
|
|
assert_eq!(reader, header);
|
|
|
|
|
|
|
|
|
@ -476,16 +477,16 @@ mod tests
|
|
|
|
|
let mut ser = Vec::new();
|
|
|
|
|
let superheader = SuperHeader::<KeyHeader>::new_for(&header);
|
|
|
|
|
println!("Writing: {:?} + {:?}", superheader, header);
|
|
|
|
|
let written = superheader.write_text(&mut ser).await?;
|
|
|
|
|
ser.extend(header.into_memory(serialise::Mode::Text)?); //header.write_text(&mut ser).await?;
|
|
|
|
|
let written = superheader.write_text(&mut ser, None).await?;
|
|
|
|
|
ser.extend(header.into_memory(serialise::Mode::Text, None)?); //header.write_text(&mut ser).await?;
|
|
|
|
|
println!("Wrote {} bytes", written);
|
|
|
|
|
println!("{}\n", ser.fmt_view());
|
|
|
|
|
|
|
|
|
|
let mut read = &ser[..];
|
|
|
|
|
let (reads, readn) = SuperHeader::from_memory(&mut read, serialise::Mode::Text)?; // SuperHeader::read_text(read).await?;
|
|
|
|
|
let (reads, readn) = SuperHeader::from_memory(&mut read, serialise::Mode::Text, |_| None)?; // SuperHeader::read_text(read).await?;
|
|
|
|
|
let mut read = &read[readn..];
|
|
|
|
|
println!("Read super: {:?}", reads);
|
|
|
|
|
let readheader = KeyHeader::read_text(&mut read).await?;
|
|
|
|
|
let readheader = KeyHeader::read_text(&mut read, |_| None).await?;
|
|
|
|
|
println!("Read real: {:?}", readheader);
|
|
|
|
|
|
|
|
|
|
reads.verify_for(&header)?;
|
|
|
|
|