Skip to content

Commit

Permalink
Add create keypair cli functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
omibo committed Apr 13, 2024
1 parent 2fe3a3a commit 17d027f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use capycrypt::{Hashable, Message, SecParam};
use capycrypt::{Hashable, KeyPair, Message, SecParam};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -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() {
Expand All @@ -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);
}
}
}
6 changes: 6 additions & 0 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 17d027f

Please sign in to comment.