#[cfg(any(feature = "rsa", feature = "ed25519"))]
use uselesskey::Factory;
fn main() {
println!("=== Tempfile Path Examples ===\n");
#[cfg(any(feature = "rsa", feature = "ed25519"))]
{
let fx = Factory::random();
#[cfg(feature = "rsa")]
rsa_example(&fx);
#[cfg(feature = "ed25519")]
ed25519_example(&fx);
}
#[cfg(not(any(feature = "rsa", feature = "ed25519")))]
{
eprintln!("Enable 'rsa' or 'ed25519' feature to run this example.");
eprintln!(" cargo run -p uselesskey --example tempfile_paths --features rsa");
}
}
#[cfg(feature = "rsa")]
fn rsa_example(fx: &Factory) {
use std::io::Write;
use uselesskey::{RsaFactoryExt, RsaSpec};
println!("--- RSA Key Fixtures ---\n");
let keypair = fx.rsa("example-service", RsaSpec::rs256());
println!("1. Built-in wrapper methods:");
let private_key_file = keypair
.write_private_key_pkcs8_pem()
.expect("failed to write private key");
let public_key_file = keypair
.write_public_key_spki_pem()
.expect("failed to write public key");
println!(" Private key: {}", private_key_file.path().display());
println!(" Public key: {}", public_key_file.path().display());
assert!(private_key_file.path().exists());
assert!(public_key_file.path().exists());
let read_back = private_key_file
.read_to_string()
.expect("failed to read private key");
assert!(
read_back.contains("-----BEGIN PRIVATE KEY-----"),
"Private key should be PEM formatted"
);
let read_back_pub = public_key_file
.read_to_string()
.expect("failed to read public key");
assert!(
read_back_pub.contains("-----BEGIN PUBLIC KEY-----"),
"Public key should be PEM formatted"
);
println!(" Verified: both files contain valid PEM data\n");
println!("2. Manual tempfile approach:");
let mut manual_file = tempfile::Builder::new()
.prefix("my-custom-prefix-")
.suffix(".key.pem")
.tempfile()
.expect("failed to create tempfile");
manual_file
.write_all(keypair.private_key_pkcs8_pem().as_bytes())
.expect("failed to write to tempfile");
manual_file.flush().expect("failed to flush");
println!(" Custom tempfile: {}", manual_file.path().display());
let manual_content = std::fs::read_to_string(manual_file.path()).expect("failed to read back");
assert!(manual_content.contains("-----BEGIN PRIVATE KEY-----"));
println!(" Verified: custom tempfile contains valid PEM data\n");
}
#[cfg(feature = "ed25519")]
fn ed25519_example(fx: &Factory) {
use uselesskey::{Ed25519FactoryExt, Ed25519Spec};
println!("--- Ed25519 Key Fixtures ---\n");
let keypair = fx.ed25519("signing-key", Ed25519Spec::default());
let private_key_file = keypair
.write_private_key_pkcs8_pem()
.expect("failed to write private key");
let public_key_file = keypair
.write_public_key_spki_pem()
.expect("failed to write public key");
println!(" Private key: {}", private_key_file.path().display());
println!(" Public key: {}", public_key_file.path().display());
let priv_content = private_key_file
.read_to_string()
.expect("failed to read private key");
let pub_content = public_key_file
.read_to_string()
.expect("failed to read public key");
assert!(priv_content.contains("-----BEGIN PRIVATE KEY-----"));
assert!(pub_content.contains("-----BEGIN PUBLIC KEY-----"));
println!(" Verified: both files contain valid PEM data\n");
}