Created
December 12, 2023 00:45
-
-
Save ansrivas/d2e505a4cf72eaf31d18db329de4b670 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{error::Error, fs::File, io::Write, path::Path}; | |
use rsa::{ | |
pkcs8::{EncodePrivateKey, EncodePublicKey, LineEnding}, | |
RsaPrivateKey, RsaPublicKey, | |
}; | |
use tracing_subscriber::EnvFilter; | |
/// Write to file if it doesn't exist. | |
fn write_to_file(path: &str, data: &str) -> Result<(), Box<dyn Error>> { | |
let path = Path::new(path); | |
if !path.exists() { | |
let mut file = File::create(path)?; | |
file.write_all(data.as_bytes())?; | |
Ok(()) | |
} else { | |
Err(From::from(format!("File {:?} already exists", path))) | |
} | |
} | |
/// Generate a private key and certificate and write them to disk. | |
pub fn generate_pem_certificate_and_write(base_path: &str) -> Result<(), Box<dyn Error>> { | |
std::fs::create_dir_all(base_path)?; | |
let mut rng = rand::thread_rng(); | |
let bits = 2048; | |
let priv_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key"); | |
let pub_key = RsaPublicKey::from(&priv_key); | |
let private_key = priv_key | |
.to_pkcs8_pem(LineEnding::LF) | |
.expect("failed to encode private key"); | |
let certificate = pub_key | |
.to_public_key_pem(LineEnding::LF) | |
.expect("failed to encode public key"); | |
let private_key_path = format!("{}/private.pem", base_path); | |
let certificate_path = format!("{}/public.pem", base_path); | |
if let Err(e) = write_to_file(&private_key_path, &private_key) { | |
tracing::debug!("File already exists, not overwriting: {}", e); | |
} | |
if let Err(e) = write_to_file(&certificate_path, &certificate) { | |
tracing::debug!("File already exists, not overwriting: {}", e); | |
} | |
tracing::info!("Successfully generated pem certificate"); | |
Ok(()) | |
} | |
fn main() { | |
tracing_subscriber::fmt() | |
.with_env_filter( | |
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")), | |
) | |
.init(); | |
let base_path = "certs"; | |
generate_pem_certificate_and_write(base_path).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment