2024-06-06 15:54:33 +01:00
|
|
|
use std::{fs::File, io::Write, path::PathBuf};
|
|
|
|
|
|
|
|
|
|
use bft_json_crdt::keypair::{make_keypair, KeyPair};
|
|
|
|
|
use pem::Pem;
|
|
|
|
|
|
|
|
|
|
/// Makes an Ed25519 keypair and returns the public and private keys as a PEM-encoded string.
|
|
|
|
|
pub(crate) fn setup() -> String {
|
|
|
|
|
let keys = make_keypair();
|
|
|
|
|
|
|
|
|
|
let public_pem = Pem::new("PUBLIC", keys.public().to_string());
|
|
|
|
|
let private_pem = Pem::new("PRIVATE", keys.private().to_string());
|
|
|
|
|
pem::encode_many(&[public_pem, private_pem])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Writes a PEM-encoded string to a file at key_path.
|
2024-06-06 16:02:00 +01:00
|
|
|
pub(crate) fn write(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
|
2024-06-06 15:54:33 +01:00
|
|
|
let mut file = File::create(key_path)?;
|
|
|
|
|
file.write(pem.to_string().as_bytes())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|