4 releases

0.0.4 Jun 20, 2024
0.0.3 Apr 12, 2024
0.0.2 Apr 12, 2024
0.0.1 Apr 10, 2024

#1932 in Network programming

Download history 12/week @ 2024-11-13 60/week @ 2024-11-20 81/week @ 2024-11-27 97/week @ 2024-12-04 31/week @ 2024-12-11 27/week @ 2024-12-18 67/week @ 2025-01-15 14/week @ 2025-01-22 73/week @ 2025-01-29 81/week @ 2025-02-05 24/week @ 2025-02-12 9/week @ 2025-02-19 10/week @ 2025-02-26

161 downloads per month
Used in hyper-noise

Unlicense

57KB
1K SLoC

tokio-noise

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

This library is sponsored by DLC.Link.

Usage

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(())
}

Dependencies

~3–11MB
~104K SLoC