use captcha_rs::{CaptchaBuilder, verify};
fn main() {
let secret = "my-super-secret-key";
println!("--- Captcha Stateless Verification Example ---");
let captcha = CaptchaBuilder::new()
.length(5)
.width(200)
.height(70)
.dark_mode(false)
.complexity(5)
.build();
println!("Generated Captcha Text: {}", captcha.text);
let (image_base64, token) = captcha.as_tuple(secret, 300)
.expect("Failed to generate stateless token. Make sure 'stateless' feature is enabled.");
println!("Base64 Image (truncated): {}...", &image_base64[..50]);
println!("Verification Token: {}", token);
println!("\n--- Verification Scenario ---");
let user_solution = captcha.text.to_lowercase(); println!("User provides solution: {}", user_solution);
let is_valid = verify(&token, &user_solution, secret)
.expect("Token was invalid (could not be decoded or secret mismatch)");
if is_valid {
println!("✅ Success: Captcha verified successfully!");
} else {
println!("❌ Failure: Captcha verification failed!");
}
let wrong_solution = "wrong123";
println!("\nUser provides wrong solution: {}", wrong_solution);
let is_valid_wrong = verify(&token, wrong_solution, secret)
.unwrap_or(false);
if is_valid_wrong {
println!("✅ Success: Captcha verified successfully!");
} else {
println!("❌ Failure: Captcha verification failed (expected)!");
}
println!("\nVerifying with wrong secret...");
let result_wrong_secret = verify(&token, &user_solution, "wrong-secret");
match result_wrong_secret {
Some(_) => println!("❌ Error: Should not have successfully decoded with wrong secret!"),
None => println!("✅ Success: Failed to decode as expected due to secret mismatch."),
}
}