Crate tokio_noise

Crate tokio_noise 

Source
Expand description

§tokio-noise

An authenticated encryption layer on top of tokio streams, driven by the Noise Protocol Framework.

This library is sponsored by DLC.Link.

use tokio::net::{TcpListener, TcpStream};
use tokio_noise::{NoiseError, NoiseTcpStream};

const PSK: [u8; 32] = [0xFF; 32];

async fn run_noise_server(tcp_stream: TcpStream) -> Result<(), NoiseError> {
    let mut noise_stream = NoiseTcpStream::handshake_responder_psk0(tcp_stream, &PSK).await?;
    let mut buf = [0u8; 1024];
    let n = noise_stream.recv(&mut buf).await?;
    assert_eq!(&buf[..n], b"hello world");
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), NoiseError> {
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr()?;

    let srv = tokio::task::spawn(async move {
        let (tcp_stream, _) = listener.accept().await?;
        run_noise_server(tcp_stream).await
    });

    // Client
    let tcp_stream = TcpStream::connect(&addr).await?;
    let mut noise_stream = NoiseTcpStream::handshake_initiator_psk0(tcp_stream, &PSK).await?;
    noise_stream.send(b"hello world").await?;

    // Wait for server to finish
    srv.await.unwrap()?;

    Ok(())
}

Re-exports§

pub use snow;

Modules§

handshakes
This module encapsulates an interface for customizing handshake protocols.

Structs§

HandshakeError
An error returned from custom handshake extension methods.
NoiseTcpStream
Represents a tokio::net::TcpStream wrapped with a layer of Noise encryption applied on top.

Enums§

NoiseError
An error derived from either std::io::Error or snow::Error.