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