|
39 | 39 | use std::fs::File; |
40 | 40 | use std::io::Write; |
41 | 41 | use std::path::PathBuf; |
| 42 | +use std::thread; |
| 43 | +use std::time::Duration; |
| 44 | +use std::sync::{Arc, atomic::{AtomicBool, Ordering}}; |
| 45 | +use std::io::{self, Write as IoWrite}; |
42 | 46 |
|
43 | 47 | use anyhow::{Context, Result}; |
44 | 48 | use clap::Parser; |
@@ -102,13 +106,39 @@ fn main() -> Result<()> { |
102 | 106 | let args = Args::parse(); |
103 | 107 |
|
104 | 108 | println!("Generating RSA key pair with {} bits...", args.length); |
| 109 | + |
| 110 | + // Flag to indicate when key generation is complete |
| 111 | + let generating = Arc::new(AtomicBool::new(true)); |
| 112 | + let generating_clone = generating.clone(); |
| 113 | + |
| 114 | + // Spawn a thread to display a spinner while generating keys |
| 115 | + let spinner_handle = thread::spawn(move || { |
| 116 | + let spinner_chars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; |
| 117 | + let mut i = 0; |
| 118 | + while generating_clone.load(Ordering::Relaxed) { |
| 119 | + print!("\r{} Generating RSA key... ", spinner_chars[i]); |
| 120 | + io::stdout().flush().ok(); |
| 121 | + i = (i + 1) % spinner_chars.len(); |
| 122 | + thread::sleep(Duration::from_millis(100)); |
| 123 | + } |
| 124 | + print!("\r \r"); // Clear the spinner line |
| 125 | + io::stdout().flush().ok(); |
| 126 | + }); |
105 | 127 |
|
106 | 128 | // Use OsRng directly to avoid dependency version conflicts |
107 | 129 | let mut rng = rsa::rand_core::OsRng; |
108 | 130 |
|
109 | 131 | // Generate a new random RSA key pair with the specified bits |
110 | 132 | let private_key = |
111 | 133 | RsaPrivateKey::new(&mut rng, args.length).context("Failed to generate RSA private key")?; |
| 134 | + |
| 135 | + // Signal that generation is complete |
| 136 | + generating.store(false, Ordering::Relaxed); |
| 137 | + // Wait for spinner thread to finish |
| 138 | + spinner_handle.join().ok(); |
| 139 | + |
| 140 | + println!("RSA key pair generation completed successfully."); |
| 141 | + |
112 | 142 | let public_key = RsaPublicKey::from(&private_key); |
113 | 143 |
|
114 | 144 | // Convert keys to PKCS#1 PEM format |
|
0 commit comments