npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

crypto-chat-rs

v1.0.3

Published

Encrypted terminal-based chat application built with Rust and Tokio

Readme

Crypto Chat RS

A secure, encrypted terminal-based chat application built with Rust and Tokio. Features real-time bidirectional communication with XOR cipher encryption over TCP.

Features

  • Real-time Communication: Asynchronous TCP-based chat using Tokio
  • Encryption: XOR cipher encryption for message privacy
  • Bidirectional Messaging: Both server and client can send and receive messages simultaneously
  • Terminal-based UI: Simple, clean command-line interface
  • Easy to Use: Run with npx or directly with Cargo

Prerequisites

  • Rust (1.70 or later): Install Rust
  • Node.js (18.0 or later): For npx wrapper (optional)

Installation

Option 1: Using npx (Recommended)

No installation needed! Just run with npx:

# Server mode
npx crypto-chat-rs listen 127.0.0.1:8080

# Client mode
npx crypto-chat-rs connect 127.0.0.1:8080

Option 2: Clone and Build

git clone <repository-url>
cd crypto-chat-rs
cargo build --release

Usage

Starting a Server

The server listens for incoming connections on a specified address and port.

With npx:

npx crypto-chat-rs listen 127.0.0.1:8080

With Cargo:

cargo run -- listen 127.0.0.1:8080

Direct binary:

./target/release/crypto-chat-rs listen 127.0.0.1:8080

Connecting as a Client

The client connects to a running server.

With npx:

npx crypto-chat-rs connect 127.0.0.1:8080

With Cargo:

cargo run -- connect 127.0.0.1:8080

Direct binary:

./target/release/crypto-chat-rs connect 127.0.0.1:8080

Default Settings

If you omit the address:port, the default is 127.0.0.1:8080:

npx crypto-chat-rs listen    # Listens on 127.0.0.1:8080
npx crypto-chat-rs connect   # Connects to 127.0.0.1:8080

How It Works

Architecture

  1. Server Mode:

    • Binds to a TCP socket and waits for a connection
    • Accepts one client connection
    • Spawns tasks for sending and receiving messages
  2. Client Mode:

    • Connects to a running server
    • Spawns tasks for sending and receiving messages
  3. Message Flow:

    • User input is read from stdin
    • Messages are encrypted using XOR cipher
    • Length-prefixed protocol ensures complete message delivery
    • Received messages are decrypted and displayed

Encryption

The application uses a simple XOR cipher for demonstration purposes:

fn xor_cipher(data: &[u8], key: &[u8]) -> Vec<u8> {
    data.iter()
        .enumerate()
        .map(|(i, &byte)| byte ^ key[i % key.len()])
        .collect()
}

Note: XOR cipher is used for educational purposes. For production use, implement proper encryption (AES-GCM, ChaCha20-Poly1305, etc.).

Protocol

Messages are sent with a length-prefix protocol:

  1. 4 bytes (big-endian u32): Message length
  2. N bytes: Encrypted message data

Example Session

Terminal 1 (Server):

$ npx crypto-chat-rs listen 127.0.0.1:8080
Listening on 127.0.0.1:8080
Connected: 127.0.0.1:54321
> Hello from server!
< Hello from client!
> How are you?

Terminal 2 (Client):

$ npx crypto-chat-rs connect 127.0.0.1:8080
Connecting to 127.0.0.1:8080
Connected!
< Hello from server!
> Hello from client!
< How are you?
> I'm good, thanks!

Development

Project Structure

crypto-chat-rs/
├── src/
│   └── main.rs          # Main application code
├── index.ts             # npx wrapper
├── package.json         # Node.js package configuration
├── Cargo.toml           # Rust package configuration
└── README.md            # This file

Building

# Debug build
cargo build

# Release build (optimized)
cargo build --release

# Using npm scripts
npm run build

Running in Development

# Using Cargo
cargo run -- listen 127.0.0.1:8080
cargo run -- connect 127.0.0.1:8080

# Using npm scripts
npm run listen
npm run connect

Technical Details

Dependencies

  • tokio: Async runtime with TCP and I/O utilities
  • std::io: Standard I/O for user input

Async Architecture

  • Uses Tokio's #[tokio::main] for async main function
  • tokio::spawn for concurrent send/receive tasks
  • tokio::sync::mpsc channel for stdin to async bridge
  • spawn_blocking for blocking stdin operations

Key Functions

  • xor_cipher(): Encrypts/decrypts messages (src/main.rs:7)
  • send_messages(): Handles user input and message sending (src/main.rs:14)
  • receive_messages(): Handles incoming message reception (src/main.rs:61)
  • server_mode(): Server initialization and connection handling (src/main.rs:88)
  • client_mode(): Client connection and setup (src/main.rs:105)

Security Considerations

WARNING: This is a demonstration project. The XOR cipher provides minimal security:

  • Not cryptographically secure: XOR with a static key is easily broken
  • No authentication: No verification of message sender
  • No forward secrecy: Compromised key compromises all messages
  • No integrity protection: Messages can be modified undetected

For Production Use:

  1. Use proper encryption (TLS, AES-GCM, ChaCha20-Poly1305)
  2. Implement key exchange (Diffie-Hellman, ECDH)
  3. Add message authentication (HMAC, AEAD)
  4. Consider using existing protocols (Signal Protocol, Noise Protocol)
  5. Use libraries like rustls, ring, or sodiumoxide

Troubleshooting

"Address already in use"

The port is occupied. Choose a different port:

npx crypto-chat-rs listen 127.0.0.1:8081

"Connection refused"

Ensure the server is running before starting the client.

Build fails

Ensure Rust is installed and up to date:

rustup update

License

MIT

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues.

Roadmap

  • [ ] Support multiple clients
  • [ ] Implement proper encryption (TLS)
  • [ ] Add user authentication
  • [ ] Chat rooms/channels
  • [ ] File transfer support
  • [ ] Message history
  • [ ] Configuration file support

Acknowledgments

Built with Rust and Tokio for learning purposes.