crypto-async-rs 0.1.3

High-performance pure Rust cryptographic library with async streaming support
Documentation
//! Example demonstrating HMAC (Hash-based Message Authentication Code)
//!
//! This example shows how to use the HMAC implementation for message authentication.

use crypto_async_rs::hmac;

fn main() {
    println!("HMAC Example");
    println!("============");

    // Test data
    let key = b"secret-key";
    let message = b"Hello, World! This is a test message for HMAC authentication.";

    println!("Key: {}", String::from_utf8_lossy(key));
    println!("Message: {}", String::from_utf8_lossy(message));

    println!("\nComputing HMAC-SHA256...");
    let hmac_sha256 = hmac::hmac_sha256(key, message);
    println!("HMAC-SHA256: {:02x?}", hmac_sha256);
    println!("HMAC-SHA256 (hex): {:02x?}", hmac_sha256);

    println!("\nComputing HMAC-SHA384...");
    let hmac_sha384 = hmac::hmac_sha384(key, message);
    println!("HMAC-SHA384: {:02x?}", hmac_sha384);
    println!("HMAC-SHA384 (hex): {:02x?}", hmac_sha384);

    println!("\nComputing HMAC-SHA512...");
    let hmac_sha512 = hmac::hmac_sha512(key, message);
    println!("HMAC-SHA512: {:02x?}", hmac_sha512);
    println!("HMAC-SHA512 (hex): {:02x?}", hmac_sha512);

    println!("\nDemonstrating key sensitivity...");
    let key2 = b"different-key";
    let hmac_sha256_key2 = hmac::hmac_sha256(key2, message);
    
    println!("HMAC with original key: {:02x?}", &hmac_sha256[..8]);
    println!("HMAC with different key: {:02x?}", &hmac_sha256_key2[..8]);
    
    if hmac_sha256 != hmac_sha256_key2 {
        println!("✅ HMAC is sensitive to key changes");
    } else {
        println!("❌ HMAC is not sensitive to key changes (this should not happen)");
    }

    println!("\nDemonstrating message sensitivity...");
    let message2 = b"Hello, World! This is a different test message.";
    let hmac_sha256_msg2 = hmac::hmac_sha256(key, message2);
    
    println!("HMAC with original message: {:02x?}", &hmac_sha256[..8]);
    println!("HMAC with different message: {:02x?}", &hmac_sha256_msg2[..8]);
    
    if hmac_sha256 != hmac_sha256_msg2 {
        println!("✅ HMAC is sensitive to message changes");
    } else {
        println!("❌ HMAC is not sensitive to message changes (this should not happen)");
    }

    println!("\nTesting edge cases...");
    
    let empty_key = b"";
    let empty_message = b"";
    let hmac_empty = hmac::hmac_sha256(empty_key, empty_message);
    println!("HMAC with empty key and message: {:02x?}", hmac_empty);

    let long_key = vec![0x42u8; 1000]; // Key longer than block size
    let hmac_long_key = hmac::hmac_sha256(&long_key, message);
    println!("HMAC with long key (1000 bytes): {:02x?}", &hmac_long_key[..8]);

    println!("\n✅ HMAC example completed successfully!");
}