https://github.com/conduition/tokio-noise
A Noise protocol encryption layer on top of tokio streams.
https://github.com/conduition/tokio-noise
Last synced: about 1 year ago
JSON representation
A Noise protocol encryption layer on top of tokio streams.
- Host: GitHub
- URL: https://github.com/conduition/tokio-noise
- Owner: conduition
- License: unlicense
- Created: 2024-04-10T16:45:23.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-17T16:07:53.000Z (over 1 year ago)
- Last Synced: 2025-04-18T00:11:51.482Z (about 1 year ago)
- Language: Rust
- Homepage:
- Size: 47.9 KB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tokio-noise
An authenticated encryption layer on top of [tokio](https://tokio.rs) streams, driven by the [Noise Protocol Framework](https://noiseprotocol.org/).
This library is [sponsored by dlcBTC](https://www.dlcbtc.com).
## Usage
```rust
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(())
}
```