feat: 生成序列号文件和存到目录

main
Zaeyee 2024-09-19 18:45:37 +08:00
parent 4756346f19
commit 1b41274823
1 changed files with 10 additions and 2 deletions

View File

@ -33,6 +33,10 @@ impl serde::Serialize for Error {
fn gen_rsa_pair(merchant_id: &str, email: &str, save_path: &str) -> Result<(), Error> { fn gen_rsa_pair(merchant_id: &str, email: &str, save_path: &str) -> Result<(), Error> {
println!("--- Begin ---"); println!("--- Begin ---");
// 创建 merchant_id 的子目录
let save_dir_path = format!("{}/{}", save_path, merchant_id);
std::fs::create_dir_all(&save_dir_path)?;
// 生成 2048 位的 RSA 私钥 // 生成 2048 位的 RSA 私钥
let rsa = Rsa::generate(2048)?; 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 bn: BigNum = BigNum::from_slice(&bytes)?;
let serial_number = Asn1Integer::from_bn(&bn)?; let serial_number = Asn1Integer::from_bn(&bn)?;
x509_builder.set_serial_number(&serial_number)?; 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_version(2)?;
x509_builder.set_subject_name(&x509_name)?; 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 格式 // 将私钥转换为 PEM 格式
let private_key_pem = private_key.private_key_to_pem_pkcs8()?; 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)?; let mut private_key_file = File::create(&private_key_path)?;
private_key_file.write_all(&private_key_pem)?; private_key_file.write_all(&private_key_pem)?;
// // 将证书转换为 PEM 格式 // // 将证书转换为 PEM 格式
let certificate_pem = x509.to_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)?; let mut certificate_file = File::create(&certificate_path)?;
certificate_file.write_all(&certificate_pem)?; certificate_file.write_all(&certificate_pem)?;