use std::fs::File;
use std::io::{self, Write};
use clap::clap_app;
use themis::keygen::gen_ec_key_pair;
fn main() {
let matches = clap_app!(keygen =>
(version: env!("CARGO_PKG_VERSION"))
(about: "Generating ECDSA key pairs.")
(@arg private: --private [path] "Private key file (default: private.key)")
(@arg public: --public [path] "Public key file (default: public.key)")
)
.get_matches();
let private_path = matches.value_of("private").unwrap_or("private.key");
let public_path = matches.value_of("public").unwrap_or("public.key");
let (private_key, public_key) = gen_ec_key_pair().split();
match write_file(&private_key, private_path) {
Ok(_) => eprintln!("wrote private key to {}", private_path),
Err(e) => eprintln!("failed to write private key to {}: {}", private_path, e),
}
match write_file(&public_key, public_path) {
Ok(_) => eprintln!("wrote public key to {}", public_path),
Err(e) => eprintln!("failed to write public key to {}: {}", public_path, e),
}
}
fn write_file<K: AsRef<[u8]>>(key: K, path: &str) -> io::Result<()> {
let mut file = File::create(path)?;
file.write_all(key.as_ref())?;
Ok(())
}