# Rust Auth Utils
A Rust port of [@better-auth/utils](https://github.com/better-auth/utils). This library provides a comprehensive set of authentication and cryptographic utilities, implemented in Rust with a focus on security and performance.
```bash
cargo add rust-auth-utils
```
## Utilities at a Glance
Utilities provided by `rust-auth-utils`:
| [**Hash**](#hash) | Hash inputs using SHA family hash functions. |
| [**HMAC**](#hmac) | Hash inputs using HMAC with a secret key. |
| [**Random**](#random) | Generate cryptographically secure random values. |
| [**RSA**](#rsa) | Perform encryption, decryption, signing, and verification with RSA keys. |
| [**ECDSA**](#ecdsa) | Perform signing and verification with ECDSA keys. |
| [**OTP**](#otp) | Generate and verify one-time passwords. |
| [**Base64**](#base64) | Encode and decode data in base64 format. |
| [**Base32**](#base32) | Encode and decode data in base32 format. |
| [**Hex**](#hex) | Encode and decode data in hexadecimal format. |
| [**Binary**](#binary) | Encode and decode data in binary format. |
## Hash
The hash module provides a way to hash input using SHA family hash functions.
```rust
use rust_auth_utils::hash::{Hash, SHAFamily};
async fn example() -> Result<(), Box<dyn std::error::Error>> {
let hash = Hash::new(SHAFamily::SHA256);
let hash_result = hash.digest("text to hash").await?;
Ok(())
}
```
## HMAC
The HMAC utility allows you to securely hash data using a secret key and SHA family hash functions.
```rust
use rust_auth_utils::hmac::HmacBuilder;
use rust_auth_utils::types::SHAFamily;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Create a new HMAC instance
let hmac = HmacBuilder::new(Some(SHAFamily::SHA256), None);
// Sign data
let signature = hmac.sign("secret-key".as_bytes(), "text to sign".as_bytes())?;
// Verify signature
let is_valid = hmac.verify("secret-key".as_bytes(), "text to sign".as_bytes(), &signature)?;
Ok(())
}
```
## Random
Generate cryptographically secure random values.
```rust
use rust_auth_utils::random::Random;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Generate random bytes
let random_bytes = Random::generate_bytes(32)?;
// Generate random string
let random_string = Random::generate_string(32, None)?;
// Generate random string with custom charset
let charset = "ABCDEF0123456789";
let random_hex = Random::generate_string(32, Some(charset))?;
Ok(())
}
```
## RSA
RSA utilities for encryption, decryption, signing, and verification.
```rust
use rust_auth_utils::rsa::{RSA, HashAlgorithm};
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Generate a new key pair
let key_pair = RSA::generate_key_pair(None, Some(HashAlgorithm::SHA256)).await?;
// Encrypt data
let encrypted = RSA::encrypt(&key_pair, "secret data").await?;
// Decrypt data
let decrypted = RSA::decrypt(&key_pair, &encrypted).await?;
// Sign data
let signature = RSA::sign(&key_pair, "message", None, None).await?;
// Verify signature
let is_valid = RSA::verify(&key_pair, &signature, "message", None, None).await?;
Ok(())
}
```
## ECDSA
ECDSA utilities for digital signatures.
```rust
use rust_auth_utils::ecdsa::ECDSA;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Generate a new key pair
let key_pair = ECDSA::generate_key_pair().await?;
// Sign data
let signature = ECDSA::sign(&key_pair, "message").await?;
// Verify signature
let is_valid = ECDSA::verify(&key_pair, &signature, "message").await?;
Ok(())
}
```
## OTP
Generate and verify TOTP (Time-based One-Time Password) and HOTP (HMAC-based One-Time Password) values.
```rust
use rust_auth_utils::otp::OTP;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Create a new OTP instance
let otp = OTP::new("your_secret", None, None);
// Generate TOTP
let totp = otp.totp().await?;
// Generate HOTP
let hotp = otp.hotp(1234).await?;
// Verify TOTP
let is_valid = otp.verify(&totp, Some(1)).await?;
// Generate QR code URL
let url = otp.url("YourApp", "user@example.com")?;
Ok(())
}
```
## Base64
Encode and decode data in base64 format.
```rust
use rust_auth_utils::base64::Base64;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Encode data
let encoded = Base64::encode("Hello, World!".as_bytes(), None)?;
// Decode data
let decoded = Base64::decode(&encoded)?;
Ok(())
}
```
## Base32
Encode and decode data in base32 format.
```rust
use rust_auth_utils::base32::Base32;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Encode data
let encoded = Base32::encode("Hello, World!".as_bytes(), Some(false))?;
// Decode data
let decoded = Base32::decode(&encoded)?;
Ok(())
}
```
## Hex
Encode and decode data in hexadecimal format.
```rust
use rust_auth_utils::hex::Hex;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Encode data
let encoded = Hex::encode("Hello, World!".as_bytes())?;
// Decode data
let decoded = Hex::decode(&encoded)?;
Ok(())
}
```
## Binary
Binary data manipulation utilities.
```rust
use rust_auth_utils::binary::Binary;
async fn example() -> Result<(), Box<dyn std::error::Error>> {
// Convert string to bytes
let bytes = Binary::encode("Hello, World!")?;
// Convert bytes to string
let text = Binary::decode(&bytes)?;
Ok(())
}
```
## License
This project is licensed under the MIT License - see the LICENSE file for details.