A secure, real-time chat application backend built with Go, featuring WebSocket connections and end-to-end encryption using AES and Diffie-Hellman key exchange.
- Real-time Communication: WebSocket-based messaging for instant communication
- End-to-End Encryption: Secure message encryption using AES-256 in CTR mode
- Diffie-Hellman Key Exchange: Secure key establishment between clients
- Lightweight Architecture: Simple and efficient Go-based server
- Cross-Origin Support: CORS-enabled for web client integration
- Algorithm: AES-256 in Counter (CTR) mode
- Key Size: 256-bit keys derived from Diffie-Hellman shared secrets
- IV Generation: Cryptographically secure random IV for each message
- Prime: 2048-bit safe prime for secure key exchange
- Generator: 2 (standard DH generator)
- Key Derivation: SHA-256 hash of shared secret for AES key
- Go 1.24.4 or higher
- Git
- Clone the repository:
git clone <repository-url>
cd chat-app-backend- Install dependencies:
go mod download- Run the server:
go run main.goThe server will start on http://localhost:8080
- Endpoint:
/ws - Protocol: WebSocket
- Description: Establishes a WebSocket connection for real-time messaging
const ws = new WebSocket('ws://localhost:8080/ws');
ws.onopen = function() {
console.log('Connected to chat server');
};
ws.onmessage = function(event) {
console.log('Received:', event.data);
};
ws.send('Hello, World!');The server currently implements a simple echo functionality. When a client sends a message, the server responds with "Echo " + the original message.
The crypto package provides utilities for implementing end-to-end encryption:
// Generate DH key pair
private, public, err := crypto.GenerateKeyPair()
// Compute shared secret (after receiving peer's public key)
sharedSecret := crypto.SharedSecretKey(private, peerPublic)
// Derive AES key
aesKey := crypto.DerivesAesKey(sharedSecret)
// Encrypt message
ciphertext, err := crypto.Encrypt([]byte("Hello, World!"), aesKey)
// Decrypt message
plaintext, err := crypto.Decrypt(ciphertext, aesKey)- Complete authentication system
- Implement message persistence
- Add user management
- Room/channel support
- Message history
- File sharing capabilities
- Rate limiting
- Comprehensive logging
I created this project as it would be a good oppurtunity for me to learn about the Diffie–Hellman key exchange algorithm so feel free to wonder around this repo and make pull requests. Let's learn together
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support, please open an issue in the GitHub repository.
Built with ❤️ using Go and modern cryptographic practices.