rust-auth-utils 1.0.0

A rust port of @better-auth/utils.
Documentation
use crate::rsa::{HashAlgorithm, RSA};
use crate::types::ExportKeyFormat;
use rsa::pkcs8::{EncodePrivateKey, EncodePublicKey};

#[tokio::test]
async fn test_generate_key_pair() {
    let key_pair = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate key pair");

    assert!(key_pair.private_key.to_pkcs8_der().is_ok());
    assert!(key_pair.public_key.to_public_key_der().is_ok());
}

#[tokio::test]
async fn test_generate_key_pair_with_options() {
    let key_pair = RSA::generate_key_pair(Some(4096), Some(HashAlgorithm::SHA512))
        .await
        .expect("Failed to generate key pair");

    assert!(key_pair.private_key.to_pkcs8_der().is_ok());
    assert!(key_pair.public_key.to_public_key_der().is_ok());
}

#[tokio::test]
async fn test_export_import_key() {
    let key_pair = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate key pair");

    let exported = RSA::export_key_public(&key_pair, ExportKeyFormat::SPKI)
        .await
        .expect("Failed to export key");

    let imported = RSA::import_key(&exported, ExportKeyFormat::SPKI, true, None)
        .await
        .expect("Failed to import key");

    assert_eq!(
        key_pair.public_key.to_public_key_der().unwrap().as_bytes(),
        imported.public_key.to_public_key_der().unwrap().as_bytes()
    );
}

#[tokio::test]
async fn test_encrypt_decrypt() {
    let key_pair = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate key pair");

    let data = b"test data";
    let encrypted = RSA::encrypt(&key_pair, data)
        .await
        .expect("Failed to encrypt data");

    let decrypted = RSA::decrypt(&key_pair, &encrypted)
        .await
        .expect("Failed to decrypt data");

    assert_eq!(data.as_ref(), decrypted.as_slice());
}

#[tokio::test]
async fn test_encrypt_decrypt_with_hash() {
    let key_pair = RSA::generate_key_pair(None, Some(HashAlgorithm::SHA512))
        .await
        .expect("Failed to generate key pair");

    let data = b"test data";
    let encrypted = RSA::encrypt(&key_pair, data)
        .await
        .expect("Failed to encrypt data");

    let decrypted = RSA::decrypt(&key_pair, &encrypted)
        .await
        .expect("Failed to decrypt data");

    assert_eq!(data.as_ref(), decrypted.as_slice());
}

#[tokio::test]
async fn test_sign_verify() {
    let key_pair = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate key pair");

    let data = b"test data";
    let signature = RSA::sign(&key_pair, data, None, None)
        .await
        .expect("Failed to sign data");

    let is_valid = RSA::verify(&key_pair, &signature, data, None, None)
        .await
        .expect("Failed to verify signature");

    assert!(is_valid);
}

#[tokio::test]
async fn test_sign_verify_with_options() {
    let key_pair = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate key pair");

    let data = b"test data";
    let signature = RSA::sign(&key_pair, data, Some(64), Some(HashAlgorithm::SHA512))
        .await
        .expect("Failed to sign data");

    let is_valid = RSA::verify(
        &key_pair,
        &signature,
        data,
        Some(64),
        Some(HashAlgorithm::SHA512),
    )
    .await
    .expect("Failed to verify signature");

    assert!(is_valid);
}

#[tokio::test]
async fn test_sign_verify_with_different_hash() {
    let key_pair = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate key pair");

    let data = b"test data";
    let signature = RSA::sign(&key_pair, data, None, Some(HashAlgorithm::SHA384))
        .await
        .expect("Failed to sign data");

    let is_valid = RSA::verify(
        &key_pair,
        &signature,
        data,
        None,
        Some(HashAlgorithm::SHA384),
    )
    .await
    .expect("Failed to verify signature");

    assert!(is_valid);
}

#[tokio::test]
async fn test_invalid_signature() {
    let key_pair1 = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate first key pair");

    let key_pair2 = RSA::generate_key_pair(None, None)
        .await
        .expect("Failed to generate second key pair");

    let data = b"test data";
    let signature = RSA::sign(&key_pair1, data, None, None)
        .await
        .expect("Failed to sign data");

    let is_valid = RSA::verify(&key_pair2, &signature, data, None, None)
        .await
        .expect("Failed to verify signature");

    assert!(!is_valid);
}