🔗Cargo.toml
[dependencies]
ssh-key = { version = "0.6", features = ["ed25519", "rsa"] }
use ssh_key::{LineEnding, PrivateKey, rand_core::OsRng};
use std::fs;
use std::path::PathBuf;
use std::time::Instant;
fn generate_ed25519_key_pair(
path_prefix: &str,
comment: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let timer = Instant::now();
let mut private_key = PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519)?;
private_key.set_comment(comment);
let private_key_bytes = private_key.to_openssh(LineEnding::LF)?;
let public_key_bytes = private_key.public_key().to_openssh()?;
let private_path = PathBuf::from(format!("{}_ed25519", path_prefix));
let public_path = PathBuf::from(format!("{}_ed25519.pub", path_prefix));
fs::write(&private_path, private_key_bytes)?;
fs::write(&public_path, public_key_bytes)?;
println!("Generated Ed25519 key pair:");
println!("Private key: {}", private_path.display());
println!("Public key: {}", public_path.display());
println!("Time Elapsed: {}ms", timer.elapsed().as_millis());
Ok(())
}
fn main() {
match generate_ed25519_key_pair("id", "example@localhost") {
Ok(_) => println!("Successfully generated Ed25519 key pair without passphrase."),
Err(e) => eprintln!("Error generating Ed25519 key pair: {}", e),
}
}