cleanup, refactor

This commit is contained in:
Janis 2024-07-30 15:50:37 +02:00
parent fe5245fc31
commit a73e2cd8d8
5 changed files with 29 additions and 15 deletions

View file

@ -25,13 +25,12 @@ required-features = ["ed25519", "clap", "rpassword", "base64"]
[dependencies] [dependencies]
rand = "0.8" rand = "0.8"
rand_chacha = "0.3"
clap = {version = "3.0.0-beta.5", optional = true, features = ["derive"]} clap = {version = "3.0.0-beta.5", optional = true, features = ["derive"]}
base64 = {version = "0.13", optional = true} base64 = {version = "0.13", optional = true}
bytes = {version = "1.1", optional = true} bytes = {version = "1.1", optional = true}
sha2 = {version = "0.9", optional = true} sha2 = {version = "0.9", optional = true}
rpassword = {version = "5.0", optional = true} rpassword = {version = "7.0", optional = true}
zeroize = {version = "1.5"} zeroize = {version = "1.8"}
argon2 = "0.5.3" argon2 = "0.5.3"
thiserror = "1.0" thiserror = "1.0"
anyhow = "1.0" anyhow = "1.0"

View file

@ -4,12 +4,12 @@ use clap::Parser;
#[derive(Parser)] #[derive(Parser)]
#[clap( #[clap(
name = "duralumin-keygen", name = "duralumin-keygen",
version = "0.2.0", version = "0.3.0",
author = "No One <noonebtw@nirgendwo.xyz>" author = "No One <noonebtw@nirgendwo.xyz>"
)] )]
struct Opts { struct Opts {
#[clap(short, long, default_value = "duralumin")] #[clap(short, long)]
file: String, file: Option<String>,
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
@ -17,6 +17,15 @@ fn main() -> anyhow::Result<()> {
println!("Generating ed25519 ssh keypair:"); println!("Generating ed25519 ssh keypair:");
let desc = libduralumin::key_gen::cli::keygen_desc_from_stdin()?; let desc = libduralumin::key_gen::cli::keygen_desc_from_stdin()?;
let base_path = opts.file.unwrap_or_else(|| {
if let Some(tag) = desc.tag.as_ref() {
format!("duralumin_{}", tag.as_str())
} else {
"duralumin".to_owned()
}
});
let keypair = libduralumin::key_gen::generate_key(desc)?; let keypair = libduralumin::key_gen::generate_key(desc)?;
println!( println!(
@ -29,8 +38,8 @@ fn main() -> anyhow::Result<()> {
keypair.randomart().render("ED25519 256", "SHA256")? keypair.randomart().render("ED25519 256", "SHA256")?
); );
let private_path = opts.file.clone(); let private_path = base_path.clone();
let public_path = opts.file.clone() + ".pub"; let public_path = base_path.clone() + ".pub";
let (private_key, public_key) = keypair.encode_keys()?; let (private_key, public_key) = keypair.encode_keys()?;
std::fs::write(&private_path, private_key)?; std::fs::write(&private_path, private_key)?;

View file

@ -5,6 +5,8 @@ use zeroize::Zeroizing;
use crate::randomart; use crate::randomart;
pub mod cli { pub mod cli {
use std::io::Write;
use zeroize::Zeroizing; use zeroize::Zeroizing;
use crate::key_gen::{HashDesc, KeygenDesc}; use crate::key_gen::{HashDesc, KeygenDesc};
@ -26,6 +28,7 @@ pub mod cli {
line line
} }
pub fn read_line() -> std::io::Result<String> { pub fn read_line() -> std::io::Result<String> {
std::io::stdout().flush()?;
let mut line = String::new(); let mut line = String::new();
std::io::stdin().read_line(&mut line)?; std::io::stdin().read_line(&mut line)?;
fix_newline_ref(&mut line); fix_newline_ref(&mut line);
@ -54,9 +57,9 @@ pub mod cli {
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
fn read_passphrase() -> Result<Zeroizing<String>> { fn read_passphrase() -> Result<Zeroizing<String>> {
let passphrase = Zeroizing::new(rpassword::prompt_password_stdout("Enter a passphrase: ")?); let passphrase = Zeroizing::new(rpassword::prompt_password("Enter a passphrase: ")?);
let passphrase2 = let passphrase2 = Zeroizing::new(rpassword::prompt_password("Re-enter your passphrase: ")?);
Zeroizing::new(rpassword::prompt_password_stdout("Enter a passphrase: ")?); std::io::stdout().flush().expect("flush stdout");
if passphrase == passphrase2 { if passphrase == passphrase2 {
println!( println!(
@ -71,6 +74,7 @@ pub mod cli {
pub fn read_argon_desc() -> Result<HashDesc> { pub fn read_argon_desc() -> Result<HashDesc> {
print!("Use argon2 variant (argon2i, argon2d, argon2id) [argon2id]: "); print!("Use argon2 variant (argon2i, argon2d, argon2id) [argon2id]: ");
let variant = match read_non_empty_line()?.as_ref().map(|s| s.as_str()) { let variant = match read_non_empty_line()?.as_ref().map(|s| s.as_str()) {
Some("argon2i") => argon2::Algorithm::Argon2i, Some("argon2i") => argon2::Algorithm::Argon2i,
Some("argon2d") => argon2::Algorithm::Argon2d, Some("argon2d") => argon2::Algorithm::Argon2d,
@ -80,7 +84,7 @@ pub mod cli {
} }
}; };
print!("Use argon2 version (16,19) [argon2id]: "); print!("Use argon2 version (16,19) [19]: ");
let version = match read_non_empty_line()?.as_ref().map(|s| s.as_str()) { let version = match read_non_empty_line()?.as_ref().map(|s| s.as_str()) {
Some("16") | Some("10") => argon2::Version::V0x10, Some("16") | Some("10") => argon2::Version::V0x10,
Some("19") | Some("13") | None => argon2::Version::V0x13, Some("19") | Some("13") | None => argon2::Version::V0x13,
@ -143,7 +147,10 @@ pub mod cli {
let tag = read_non_empty_line()?.map(|s| Zeroizing::new(s)); let tag = read_non_empty_line()?.map(|s| Zeroizing::new(s));
print!("Encrypt keypair with passphrase? [Y/n]: "); print!("Encrypt keypair with passphrase? [Y/n]: ");
let encrypt = read_line()? == "Y"; let encrypt = read_line()? != "n";
if encrypt {
print!("Will encrypt keypair.");
}
print!("Use hash algorithm (sha256, argon2) [argon2]: "); print!("Use hash algorithm (sha256, argon2) [argon2]: ");
let hash = match read_non_empty_line()?.as_ref().map(|s| s.as_str()) { let hash = match read_non_empty_line()?.as_ref().map(|s| s.as_str()) {
@ -225,7 +232,7 @@ impl KeyPair {
} }
let private_key = Zeroizing::new(core::str::from_utf8(&private_key).unwrap().to_string()); let private_key = Zeroizing::new(core::str::from_utf8(&private_key).unwrap().to_string());
let public_key = public_key.public_key_base64(); let public_key = format!("{} {}", public_key.name(), public_key.public_key_base64());
Ok((private_key, public_key)) Ok((private_key, public_key))
} }
} }

View file

@ -6,7 +6,6 @@ pub mod passphrase_gen;
#[cfg(feature = "password-gen")] #[cfg(feature = "password-gen")]
pub mod password_gen; pub mod password_gen;
#[path = "ed25519.rs"]
pub mod randomart; pub mod randomart;
#[cfg(feature = "ed25519")] #[cfg(feature = "ed25519")]