oxidite-security
Security utilities and primitives for the Oxidite web framework. Provides cryptographic functions, secure random generation, input sanitization, and security best practices.
Overview
oxidite-security provides a comprehensive set of security utilities and primitives for the Oxidite web framework. It includes cryptographic functions, secure random generation, input sanitization, and other security-related utilities that follow best practices and industry standards.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Features
- Cryptographic functions - Secure hashing, encryption, and digital signatures
- Password hashing - Industry-standard password hashing with Argon2, bcrypt, or scrypt
- Secure random generation - Cryptographically secure random number and token generation
- Input sanitization - Protection against XSS, SQL injection, and other injection attacks
- Token generation - Secure JWT and custom token creation
- Hashing utilities - Various hashing algorithms for different use cases
- Data encryption - Symmetric and asymmetric encryption utilities
- Security headers - Helper functions for setting security headers
- Rate limiting - Protection against brute-force and DoS attacks
- CSP utilities - Content Security Policy helper functions
Usage
Password Hashing
Securely hash and verify passwords:
use ;
// Create a hasher with Argon2 algorithm (recommended)
let hasher = new;
// Hash a password
let password_hash = hasher.hash?;
// Verify a password against its hash
let is_valid = hasher.verify?;
if is_valid else
// You can also use bcrypt or scrypt
let bcrypt_hasher = new;
let bcrypt_hash = bcrypt_hasher.hash?;
Secure Random Generation
Generate cryptographically secure random values:
use *;
// Generate a secure random token
let token = generate_secure_token; // 32 bytes = 256 bits
println!;
// Generate secure random bytes
let random_bytes = generate_random_bytes;
println!;
// Generate a secure random number in range
let random_num = generate_random_u32_in_range;
println!;
// Generate a secure random hex string
let hex_string = generate_random_hex; // 32 bytes in hex
println!;
Cryptographic Hashing
Compute secure hashes:
use *;
// SHA-256 hashing
let sha256_hash = hash_sha256;
println!;
// Blake3 hashing (faster alternative)
let blake3_hash = hash_blake3;
println!;
// HMAC with secret key
let hmac = compute_hmac_sha256;
println!;
// PBKDF2 for password stretching
let pbkdf2_hash = pbkdf2_hash;
println!;
Input Sanitization
Protect against XSS and injection attacks:
use *;
// HTML escape user input
let user_input = "<script>alert('xss')</script>";
let safe_html = html_escape;
println!; // <script>alert('xss')</script>
// Sanitize user input for database queries
let user_search = "'; DROP TABLE users; --";
let sanitized = sql_sanitize;
println!;
// URL encode user input
let unsafe_url = "https://example.com/search?q=user input & more";
let encoded = url_encode;
println!;
// Sanitize file names
let unsafe_filename = "../../../etc/passwd";
let safe_filename = sanitize_filename;
println!;
JWT Token Creation and Verification
Create and verify JSON Web Tokens:
use ;
// Create a signer with HS256 algorithm
let signer = new;
// Create claims
let claims = json!;
// Sign the token
let token = signer.sign?;
println!;
// Verify the token
let verifier = new;
let verified_claims = verifier.verify?;
println!;
Content Security Policy
Generate Content Security Policy headers:
use *;
let mut csp = new;
// Restrict sources
csp.default_src
.script_src
.style_src
.img_src
.font_src;
let csp_header = csp.build;
println!;
// Output: "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' https:; font-src 'self'"
Rate Limiting Utilities
Implement rate limiting to prevent abuse:
use *;
// Create a rate limiter (100 requests per minute per IP)
let mut limiter = new;
// Check if a request should be allowed
let client_ip = "192.168.1.1";
if limiter.is_allowed else
Encryption Utilities
Encrypt and decrypt data:
use *;
// Generate a key for symmetric encryption
let key = generate_aes_key;
let iv = generate_iv;
// Encrypt data
let plaintext = "Sensitive data";
let ciphertext = encrypt_aes_gcm?;
// Decrypt data
let decrypted = decrypt_aes_gcm?;
let decrypted_str = Stringfrom_utf8?;
println!;
Security Headers
Helper functions for setting security headers:
use *;
// Create security headers
let security_headers = new
.strict_transport_security
.x_frame_options
.x_content_type_options
.x_xss_protection
.referrer_policy;
// Apply to HTTP response (integration with your framework)
// response.headers_mut().extend(security_headers.into_iter());
Integration with Oxidite
The security utilities integrate seamlessly with Oxidite applications:
use *;
use Hasher;
async
async
Security Best Practices
The library implements security best practices:
- Defense in depth: Multiple layers of protection
- Principle of least privilege: Minimal permissions by default
- Secure defaults: Safe configurations out of the box
- Constant-time operations: Protection against timing attacks
- Proper entropy: High-quality randomness for security-sensitive operations
- Memory safety: No memory leaks or buffer overflows
Performance Considerations
Security operations are optimized for performance:
- Asynchronous operations where appropriate
- Efficient algorithms that balance security and performance
- Proper resource management
- Minimal overhead for security checks
License
MIT