From 17d027f9bee14aa444a2279e8010363229ac79ef Mon Sep 17 00:00:00 2001 From: omibo Date: Fri, 12 Apr 2024 22:18:31 -0600 Subject: [PATCH] Add create keypair cli functionality --- Cargo.lock | 1 + Cargo.toml | 2 +- src/lib.rs | 5 ++++- src/main.rs | 34 +++++++++++++++++++++++++++++++++- src/ops.rs | 6 ++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc3a1e7..7b7c227 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -103,6 +103,7 @@ dependencies = [ "rand", "rayon", "serde", + "serde_json", "structopt", "tiny_ed448_goldilocks", ] diff --git a/Cargo.toml b/Cargo.toml index 368c0fa..415a8d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ aes = "0.8.3" rayon = "1.5" structopt = "0.3" serde = { version = "1.0", features = ["derive"] } - +serde_json = "1.0" [[bench]] name = "benchmark_sha3" diff --git a/src/lib.rs b/src/lib.rs index 9ba4a95..883801a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,9 @@ /// Elliptic curve backend use tiny_ed448_goldilocks::curve::{extended_edwards::ExtendedPoint, field::scalar::Scalar}; +/// Serializing data structures +use serde::Serialize; + /// Module for SHA-3 primitives pub mod sha3 { @@ -53,7 +56,7 @@ pub struct Signature { pub z: Scalar, } -#[derive(Debug, Clone)] +#[derive(Serialize, Debug, Clone)] /// An object containing the fields necessary to represent an asymmetric keypair. pub struct KeyPair { /// String indicating the owner of the key, can be arbitrary diff --git a/src/main.rs b/src/main.rs index f978a21..fef55a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use capycrypt::{Hashable, Message, SecParam}; +use capycrypt::{Hashable, KeyPair, Message, SecParam}; use structopt::StructOpt; #[derive(Debug, StructOpt)] @@ -20,6 +20,24 @@ enum Command { )] bits: usize, }, + + #[structopt(name = "new_keypair")] + NewKeypair { + #[structopt(help = "Password")] + pw: String, + + #[structopt(help = "Owner of the key pair")] + owner: String, + + #[structopt(help = "Selected curve")] + _curve: String, + + #[structopt(help = "Security length", short, long, default_value = "256")] + bits: usize, + + #[structopt(help = "Output file name", short, long)] + output: String, + }, } fn main() { @@ -37,5 +55,19 @@ fn main() { Err(_) => eprintln!("Error: Hash computation failed"), } } + + Command::NewKeypair { + pw, + owner, + _curve, + bits, + output, + } => { + let sec_param = SecParam::from_int(bits).expect("Unsupported security parameter."); + let kp = KeyPair::new(pw.as_bytes(), owner, &sec_param) + .expect("Unable to generate the requested key pair"); + + let _ = kp.save_to_file(&output); + } } } diff --git a/src/ops.rs b/src/ops.rs index 92b76a2..dfae03f 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -344,6 +344,12 @@ impl KeyPair { date_created: get_date_and_time_as_string(), }) } + + /// Documentation should be written + pub fn save_to_file(&self, filename: &str) -> std::io::Result<()> { + let json_key_pair = serde_json::to_string_pretty(self).unwrap(); + std::fs::write(filename, json_key_pair) + } } impl KeyEncryptable for Message {