A Rust implementation of XPFF header generation using AES-256-GCM encryption. This library provides secure header generation for authentication and data protection using a combination of SHA-256 key derivation and AES-256-GCM authenticated encryption.
This project is based on the original work from dsekz/twitter-x-xp-forwarded-for-header. Special thanks to @dsekz for creating the foundation that made this project possible.
- Repository: twitter-x-xp-forwarded-for-header
- Author: @dsekz
All modifications and enhancements in this fork are my own work. Please refer to the original repository for the initial implementation and documentation.
- π AES-256-GCM encryption with authenticated encryption
- π SHA-256 key derivation from base key and guest ID
- π² Secure random nonce generation for each encryption
- π¦ Pure Rust implementation with no unsafe code
- π¦ Simple API with both struct-based and function-based interfaces
- β‘ Zero-copy operations where possible
- π§ͺ Comprehensive test coverage
- π Full documentation with examples
Add this to your Cargo.toml:
[dependencies]
xpff-header-generator = "0.1.0"use xpff_header_generator::XPFFHeaderGenerator;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a generator with your base key
let generator = XPFFHeaderGenerator::new("your_secret_base_key");
// Encrypt a message
let encrypted = generator.generate_xpff("Hello, World!", "guest123")?;
println!("Encrypted: {}", encrypted);
// Decrypt the message
let decrypted = generator.decode_xpff(&encrypted, "guest123")?;
println!("Decrypted: {}", decrypted);
Ok(())
}use xpff_header_generator::XPFFHeaderGenerator;
let base_key = "0e6be1f1e21ffc33590b888fd4dc81b19713e570e805d4e5df80a493c9571a05";
let generator = XPFFHeaderGenerator::new(base_key);
let guest_id = "v1%3A174849298500261196";
let message = r#"{"user_id": 123, "timestamp": 1234567890}"#;
// Generate encrypted header
let encrypted = generator.generate_xpff(message, guest_id)?;
// Decode the header
let decrypted = generator.decode_xpff(&encrypted, guest_id)?;
assert_eq!(message, decrypted);use xpff_header_generator::{generate_xpff, decode_xpff};
let encrypted = generate_xpff("Hello", "guest123", "base_key")?;
let decrypted = decode_xpff(&encrypted, "guest123", "base_key")?;use xpff_header_generator::{XPFFHeaderGenerator, XPFFError};
let generator = XPFFHeaderGenerator::new("base_key");
match generator.decode_xpff("invalid_data", "guest123") {
Ok(decrypted) => println!("Decrypted: {}", decrypted),
Err(XPFFError::HexError(e)) => eprintln!("Invalid hex: {}", e),
Err(XPFFError::DecryptionError(e)) => eprintln!("Decryption failed: {}", e),
Err(e) => eprintln!("Error: {}", e),
}- Key Derivation: The library derives a 256-bit encryption key by concatenating the base key with the guest ID and computing SHA-256 hash
- Encryption: Uses AES-256-GCM with a random 96-bit nonce for authenticated encryption
- Output Format: Returns hex-encoded string containing:
nonce (12 bytes) || ciphertext || auth_tag (16 bytes)
The main struct for generating and decoding XPFF headers.
new(base_key: impl Into<String>) -> Self- Creates a new generator instancegenerate_xpff(&self, plaintext: &str, guest_id: &str) -> Result<String, XPFFError>- Encrypts a messagedecode_xpff(&self, hex_string: &str, guest_id: &str) -> Result<String, XPFFError>- Decrypts a message
generate_xpff(plaintext: &str, guest_id: &str, base_key: &str) -> Result<String, XPFFError>decode_xpff(hex_string: &str, guest_id: &str, base_key: &str) -> Result<String, XPFFError>
The library uses a custom XPFFError enum for error handling:
EncryptionError- Encryption operation failedDecryptionError- Decryption operation failedHexError- Invalid hex encodingUtf8Error- Invalid UTF-8 in decrypted dataInvalidDataLength- Data too short to be valid
- Each encryption uses a unique random nonce
- AES-256-GCM provides authenticated encryption
- Keys are derived using SHA-256
- The base key should be kept secret and stored securely
- Guest IDs should be unique per user/session
The library is designed for performance:
- Zero-copy operations where possible
- Efficient memory allocation
- Minimal dependencies
cargo build --releasecargo testcargo benchContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the Python implementation using PyCrypto
- Built with excellent Rust cryptography libraries
If you have any questions or run into issues, please open an issue on the GitHub repository.