use apple_cryptokit::{
Result,
asymmetric::p256::{generate_keypair, sign, verify},
authentication::hmac_sha256,
hashing::{HashAlgorithm, HashFunction, SHA256, sha256_hash},
key_derivation::hkdf_sha256_derive,
quantum::{
DigitalSignatureAlgorithm, KEMPrivateKey, KEMPublicKey, KeyEncapsulationMechanism, MLDsa65,
MLKem768, SignaturePrivateKey, SignaturePublicKey, XWingMLKem768X25519,
},
symmetric::aes::{aes_gcm_decrypt, aes_gcm_encrypt},
};
fn main() -> Result<()> {
println!("Apple CryptoKit for Rust - Basic Usage Examples");
println!("===================================================\n");
hashing_examples()?;
authentication_examples()?;
symmetric_encryption_examples()?;
key_derivation_examples()?;
asymmetric_examples()?;
post_quantum_examples()?;
println!("\nAll examples completed successfully!");
Ok(())
}
fn hashing_examples() -> Result<()> {
println!("Hashing Examples");
println!("------------------");
let data = b"Hello, Apple CryptoKit!";
let hash = sha256_hash(data);
println!("SHA256 hash: {}", hex::encode(hash));
let hash_trait = SHA256::hash(data);
println!("SHA256 (trait): {}", hex::encode(hash_trait));
let algorithm = HashAlgorithm::Sha256;
let dynamic_hash = algorithm.compute(data);
println!("SHA256 (dynamic): {}\n", hex::encode(dynamic_hash));
Ok(())
}
fn authentication_examples() -> Result<()> {
println!("Message Authentication Examples");
println!("----------------------------------");
let key = b"my-secret-key";
let message = b"Important message to authenticate";
let mac = hmac_sha256(key, message)?;
println!("HMAC-SHA256: {}", hex::encode(mac));
let verify_mac = hmac_sha256(key, message)?;
let is_valid = mac == verify_mac;
println!(
"HMAC verification: {}\n",
if is_valid { "Valid" } else { "❌ Invalid" }
);
Ok(())
}
fn symmetric_encryption_examples() -> Result<()> {
println!("Symmetric Encryption Examples");
println!("---------------------------------");
let key = b"0123456789abcdef0123456789abcdef"; let nonce = b"unique12byte"; let plaintext = b"This is a secret message that needs encryption!";
let ciphertext = aes_gcm_encrypt(key, nonce, plaintext)?;
println!("Plaintext: {}", String::from_utf8_lossy(plaintext));
println!("Ciphertext: {}", hex::encode(&ciphertext));
let decrypted = aes_gcm_decrypt(key, nonce, &ciphertext)?;
println!("Decrypted: {}", String::from_utf8_lossy(&decrypted));
let success = plaintext == decrypted.as_slice();
println!(
"Encryption/Decryption: {}\n",
if success { "Success" } else { "Failed" }
);
Ok(())
}
fn key_derivation_examples() -> Result<()> {
println!("Key Derivation Examples");
println!("--------------------------");
let input_key_material = b"shared-secret-from-key-exchange";
let salt = b"random-salt";
let info = b"application-specific-context";
let output_length = 32;
let derived_key = hkdf_sha256_derive(input_key_material, salt, info, output_length)?;
println!("Input key material: {}", hex::encode(input_key_material));
println!("Derived key (32 bytes): {}\n", hex::encode(derived_key));
Ok(())
}
fn asymmetric_examples() -> Result<()> {
println!("Asymmetric Cryptography Examples");
println!("------------------------------------");
let (private_key, public_key) = generate_keypair()?;
println!("Generated P-256 key pair");
let message = b"Document to be signed";
let signature = sign(&private_key, message)?;
println!("Message: {}", String::from_utf8_lossy(message));
println!("Signature: {}", hex::encode(signature.as_bytes()));
let is_valid = verify(&public_key, &signature, message)?;
println!(
"Signature verification: {}\n",
if is_valid { "Valid" } else { "Invalid" }
);
Ok(())
}
fn post_quantum_examples() -> Result<()> {
println!("Post-Quantum Cryptography Examples");
println!("--------------------------------------");
println!("ML-KEM768 (Quantum-resistant KEM):");
let kem_private = MLKem768::generate_private_key()?;
let kem_public = kem_private.public_key();
let (ciphertext, shared_secret) = kem_public.encapsulate()?;
let decapsulated_secret = kem_private.decapsulate(&ciphertext)?;
let kem_success = shared_secret == decapsulated_secret;
println!(" Shared secret length: {} bytes", shared_secret.len());
println!(
" Encapsulation/Decapsulation: {}",
if kem_success { "Success" } else { "Failed" }
);
println!("\nX-Wing (Hybrid ML-KEM768 + X25519):");
let xwing_private = XWingMLKem768X25519::generate_private_key()?;
let xwing_public = xwing_private.public_key();
let (xwing_ciphertext, xwing_secret) = xwing_public.encapsulate()?;
let xwing_decapsulated = xwing_private.decapsulate(&xwing_ciphertext)?;
let xwing_success = xwing_secret == xwing_decapsulated;
println!(" Shared secret length: {} bytes", xwing_secret.len());
println!(
" Hybrid KEM: {}",
if xwing_success { "Success" } else { "Failed" }
);
println!("\nML-DSA65 (Quantum-resistant signatures):");
let dsa_private = MLDsa65::generate_private_key()?;
let dsa_public = dsa_private.public_key();
let pq_message = b"Hello, post-quantum world!";
let pq_signature = dsa_private.sign(pq_message)?;
let pq_valid = dsa_public.verify(pq_message, &pq_signature)?;
println!(" Message: {}", String::from_utf8_lossy(pq_message));
println!(" Signature length: {} bytes", pq_signature.len());
println!(
" Signature verification: {}\n",
if pq_valid { "Valid" } else { "Invalid" }
);
Ok(())
}