From ee89469b3f324a49f169e1b321844942a187bff6 Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 20 Apr 2021 02:01:02 +0100 Subject: [PATCH] eh, idk if this is getting anywhere --- src/crypt.rs | 5 +++++ src/stream.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/crypt.rs b/src/crypt.rs index 533973d..a986111 100644 --- a/src/crypt.rs +++ b/src/crypt.rs @@ -5,3 +5,8 @@ pub type RsaPrivateKey = (); /// TODO: RSA public key pub type RsaPublicKey = (); + +pub(crate) fn generate() -> RsaPrivateKey +{ + todo!() +} diff --git a/src/stream.rs b/src/stream.rs index 5d73571..2bd3379 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -54,9 +54,12 @@ impl Split for (T, U) (a, b) } } +//TODO: Add trait `SplitRef` for exchange, I guess? /// Combined Read + Write encryptable async stream. /// +/// The `AsyncRead` and `AsyncWrite` impls of this type forward to the backing impls for `S`. +/// /// # Exchange /// A combined stream is the only way to exchange pubkeys and enabling the creation of encrypted read/write wrappers on the combined stream or splits. #[pin_project] @@ -67,7 +70,50 @@ pub struct Stream #[pin] stream: S, } -impl Split for Stream +impl Stream +where S: Split, + S::First: AsyncWrite, + S::Second: AsyncRead +{ + /// Create a new `Stream` from two streams, one implemetor of `AsyncWrite`, and one of `AsyncRead`. + pub fn new(tx: S::First, rx: S::Second) -> Self + { + Self { + meta: EncryptedStreamMeta { + them: None, + us: crypt::generate(), + }, + stream: S::unsplit(tx, rx), + } + } +} + +impl Stream +{ + /// Create a new `Stream` from an implementor of both `AsyncRead` and `AsyncWrite`. + pub fn new_single(stream: S) -> Self + { + Self { + meta: EncryptedStreamMeta { + them: None, + us: crypt::generate(), + }, + stream, + } + } + + /// Create a split by cloning `S`. + pub fn split_clone(self) -> (WriteHalf, ReadHalf) + where S: Clone + { + Stream { + stream: (self.stream.clone(), self.stream), + meta: self.meta + }.split() + } +} + +impl Split for Stream where S: Split, S::First: AsyncWrite, S::Second: AsyncRead @@ -83,7 +129,7 @@ where S: Split, } } -impl Stream +impl Stream where S: Split, S::First: AsyncWrite, S::Second: AsyncRead