From 1b4127482395003d2fad139165259f0b421af5f7 Mon Sep 17 00:00:00 2001 From: zaeyee Date: Thu, 19 Sep 2024 18:45:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=9F=E6=88=90=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8F=B7=E6=96=87=E4=BB=B6=E5=92=8C=E5=AD=98=E5=88=B0=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4a67d66..61ecca3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -33,6 +33,10 @@ impl serde::Serialize for Error { fn gen_rsa_pair(merchant_id: &str, email: &str, save_path: &str) -> Result<(), Error> { println!("--- Begin ---"); + // 创建 merchant_id 的子目录 + let save_dir_path = format!("{}/{}", save_path, merchant_id); + std::fs::create_dir_all(&save_dir_path)?; + // 生成 2048 位的 RSA 私钥 let rsa = Rsa::generate(2048)?; // 创建私钥 @@ -52,6 +56,10 @@ fn gen_rsa_pair(merchant_id: &str, email: &str, save_path: &str) -> Result<(), E let bn: BigNum = BigNum::from_slice(&bytes)?; let serial_number = Asn1Integer::from_bn(&bn)?; x509_builder.set_serial_number(&serial_number)?; + // 保存序列号到文件 + let serial_number_path = format!("{}/{}_serial_number.txt", save_dir_path, merchant_id); + let mut serial_number_file = File::create(&serial_number_path)?; + serial_number_file.write_all(bn.to_hex_str()?.as_bytes())?; x509_builder.set_version(2)?; x509_builder.set_subject_name(&x509_name)?; @@ -67,14 +75,14 @@ fn gen_rsa_pair(merchant_id: &str, email: &str, save_path: &str) -> Result<(), E // 将私钥转换为 PEM 格式 let private_key_pem = private_key.private_key_to_pem_pkcs8()?; // 保存私钥到文件 - let private_key_path = format!("{}/{}_private_key.pem", save_path, merchant_id); + let private_key_path = format!("{}/{}_private_key.pem", save_dir_path, merchant_id); let mut private_key_file = File::create(&private_key_path)?; private_key_file.write_all(&private_key_pem)?; // // 将证书转换为 PEM 格式 let certificate_pem = x509.to_pem()?; // 保存证书到文件 - let certificate_path = format!("{}/{}_certificate.pem", save_path, merchant_id); + let certificate_path = format!("{}/{}_certificate.pem", save_dir_path, merchant_id); let mut certificate_file = File::create(&certificate_path)?; certificate_file.write_all(&certificate_pem)?;