Herolib Crypt
Simple and secure cryptography library for Rust.
Features
-
Asymmetric Cryptography
- Ed25519 digital signatures (signing/verification)
- X25519 ECDH encryption (encrypt/decrypt)
- Dual keypair architecture (separate keys for signing and encryption)
-
Symmetric Cryptography
- XChaCha20-Poly1305 authenticated encryption
- Argon2id password-based key derivation
- Secure key handling with automatic memory zeroization
Quick Start
Asymmetric: Generate a Keypair
use generate_keypair;
let keypair = generate_keypair?;
// For signing (Ed25519)
println!;
println!;
// For encryption (X25519)
println!;
println!;
Asymmetric: Sign and Verify Messages
use ;
// Generate keypair
let keypair = generate_keypair?;
// Sign a message (uses Ed25519 signing key)
let message = "Hello, world!";
let signature = sign_message?;
// Verify the signature (uses Ed25519 public key)
let is_valid = verify_signature?;
assert!;
Asymmetric: Encrypt and Decrypt Messages
use ;
// Alice and Bob generate their keypairs
let alice = generate_keypair?;
let bob = generate_keypair?;
// Alice encrypts a message for Bob (uses Bob's X25519 encryption public key)
let message = "Secret message";
let encrypted = encrypt_message?;
// Bob decrypts with his X25519 encryption private key
let decrypted = decrypt_message?;
assert_eq!;
Symmetric: Password-Based Encryption (Recommended)
use ;
// Encrypt with just a password - salt is handled automatically!
let encrypted = encrypt_with_password?;
// Decrypt with the same password
let decrypted = decrypt_with_password?;
assert_eq!;
Symmetric: String Encryption
use ;
// Encrypt string to base64
let encrypted = encrypt_string?;
// Decrypt back to string
let decrypted = decrypt_string?;
assert_eq!;
Symmetric: Advanced - Random Key
use ;
// Generate a random key (for programmatic key management)
let key = generate;
let cipher = new;
// Encrypt and decrypt
let encrypted = cipher.encrypt?;
let decrypted = cipher.decrypt?;
API Reference
Asymmetric Key Generation
generate_keypair() -> CryptoResult<KeyPair>- Generate a new dual keypair (Ed25519 + X25519)public_key_from_private(private_key_hex) -> CryptoResult<String>- Derive Ed25519 public keyencryption_public_key_from_private(encryption_private_key_hex) -> CryptoResult<String>- Derive X25519 public key
Asymmetric Signing (Ed25519)
sign_message(message, private_key_hex) -> CryptoResult<String>- Sign a messageverify_signature(message, signature_hex, public_key_hex) -> CryptoResult<bool>- Verify a signature
Asymmetric Encryption (X25519 + ChaCha20-Poly1305)
encrypt_message(message, recipient_encryption_public_key_hex) -> CryptoResult<String>- Encrypt for a recipientdecrypt_message(encrypted_hex, encryption_private_key_hex) -> CryptoResult<String>- Decrypt a message
Symmetric Encryption (Simple API)
encrypt_with_password(data, password) -> SymmetricResult<Vec<u8>>- Encrypt with passworddecrypt_with_password(encrypted, password) -> SymmetricResult<Vec<u8>>- Decrypt with passwordencrypt_string(text, password) -> SymmetricResult<String>- Encrypt string to base64decrypt_string(encrypted_base64, password) -> SymmetricResult<String>- Decrypt base64 to string
Symmetric Encryption (Advanced API)
EncryptionKey::generate() -> EncryptionKey- Generate a random 256-bit keyCipher::new(key) -> Cipher- Create a cipherCipher::encrypt(plaintext) -> SymmetricResult<Vec<u8>>- Encrypt dataCipher::decrypt(ciphertext) -> SymmetricResult<Vec<u8>>- Decrypt data
Key Formats
All keys are represented as hex strings:
| Key Type | Size (bytes) | Hex Length |
|---|---|---|
| Ed25519 Private Key | 32 | 64 chars |
| Ed25519 Public Key | 32 | 64 chars |
| X25519 Private Key | 32 | 64 chars |
| X25519 Public Key | 32 | 64 chars |
| Ed25519 Signature | 64 | 128 chars |
Security Properties
Signing (Ed25519)
- Deterministic: Same message + key = same signature
- Unforgeable: Cannot create valid signature without the private key
- Verifiable: Anyone with public key can verify authenticity
Encryption (X25519 + ChaCha20-Poly1305)
- Confidential: Only recipient's private key can decrypt
- Authenticated: Tampering is detected (AEAD)
- Forward Secure: Each message uses ephemeral keys
Symmetric (XChaCha20-Poly1305)
- Authenticated: Tampering detection via Poly1305 MAC
- Large Nonce: 192-bit nonces prevent collisions
- Memory Safe: Keys are automatically zeroized on drop
Dependencies
ed25519-dalek- Ed25519 signaturescurve25519-dalek- X25519 key exchangechacha20poly1305- AEAD encryptionsha3- Key derivation (KDF)argon2- Password-based key derivationzeroize- Secure memory handling
Testing
Run all tests:
Or use the helper script:
Building
Specifications
See the detailed specifications:
- SPEC_SIGNING.md - Signing specification
- SPEC_ENCRYPTION.md - Encryption specification
License
Apache-2.0