mirror of
https://github.com/nelsbrock/fingerprunk.git
synced 2026-08-15 00:16:47 +02:00
Compare commits
7
Commits
0.3.0
...
20c0a919c5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20c0a919c5
|
||
|
|
3ea0de1cdb
|
||
|
|
9f992caef9
|
||
|
|
c8bedabe1f
|
||
|
|
e8b83d41cb
|
||
|
|
6af66fd292
|
||
|
|
bc07cba585
|
Generated
-17
@@ -481,7 +481,6 @@ dependencies = [
|
||||
"ctrlc",
|
||||
"fancy-regex",
|
||||
"num-integer",
|
||||
"num_cpus",
|
||||
"rpassword",
|
||||
"sequoia-openpgp",
|
||||
]
|
||||
@@ -605,12 +604,6 @@ version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.65"
|
||||
@@ -994,16 +987,6 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2"
|
||||
version = "0.6.4"
|
||||
|
||||
@@ -15,6 +15,5 @@ clap = { version = "4.6.1", features = ["derive"] }
|
||||
ctrlc = "3.5.2"
|
||||
fancy-regex = "0.18.0"
|
||||
num-integer = "0.1.46"
|
||||
num_cpus = "1.17.0"
|
||||
rpassword = "7.5.2"
|
||||
sequoia-openpgp = "2.2.0"
|
||||
|
||||
+11
-15
@@ -3,7 +3,7 @@
|
||||
use std::{
|
||||
fmt::{self, Write},
|
||||
io,
|
||||
num::NonZeroU64,
|
||||
num::NonZero,
|
||||
sync::{
|
||||
atomic::{AtomicBool, AtomicU64, Ordering},
|
||||
mpsc,
|
||||
@@ -38,9 +38,10 @@ enum Message {
|
||||
pub struct Config {
|
||||
pub regex: Regex,
|
||||
pub status_enabled: bool,
|
||||
pub stop_after: Option<NonZeroU64>,
|
||||
pub stop_after: Option<NonZero<u64>>,
|
||||
pub password: Option<Password>,
|
||||
pub userids: Vec<UserID>,
|
||||
pub workers: NonZero<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -83,24 +84,20 @@ impl Fingerprunk {
|
||||
}
|
||||
|
||||
thread::scope(|scope| {
|
||||
let ref_self = &self;
|
||||
|
||||
let status_displayer = if self.config.status_enabled {
|
||||
Some(
|
||||
thread::Builder::new()
|
||||
.name("status_displayer".to_string())
|
||||
.spawn_scoped(scope, move || ref_self.status_displayer_thread())?,
|
||||
.spawn_scoped(scope, || self.status_displayer_thread())?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
for num in 0..num_cpus::get() {
|
||||
let sender = sender.clone();
|
||||
|
||||
for num in 0..self.config.workers.get() {
|
||||
thread::Builder::new()
|
||||
.name(format!("worker-{num:03}"))
|
||||
.spawn_scoped(scope, move || ref_self.worker_thread(sender))?;
|
||||
.spawn_scoped(scope, || self.worker_thread(&sender))?;
|
||||
}
|
||||
|
||||
let mut stdout = io::stdout().lock();
|
||||
@@ -109,8 +106,8 @@ impl Fingerprunk {
|
||||
for message in receiver {
|
||||
match message {
|
||||
Message::Key(key) => {
|
||||
let cert = self.key_to_cert(&key)?;
|
||||
self.serialize_cert(cert, &mut stdout)?;
|
||||
let cert = self.key_to_cert(key)?;
|
||||
self.serialize_cert(&cert, &mut stdout)?;
|
||||
|
||||
// Increase "found" counter and stop if enough matches have been found
|
||||
let prev = self.counter_found.fetch_add(1, Ordering::Relaxed);
|
||||
@@ -134,7 +131,7 @@ impl Fingerprunk {
|
||||
})
|
||||
}
|
||||
|
||||
fn worker_thread(&self, sender: mpsc::SyncSender<Message>) {
|
||||
fn worker_thread(&self, sender: &mpsc::SyncSender<Message>) {
|
||||
let mut fingerprint_hex = String::with_capacity(20 * 2);
|
||||
|
||||
while !self.stop.load(Ordering::Relaxed) {
|
||||
@@ -160,7 +157,7 @@ impl Fingerprunk {
|
||||
.expect("should check regex without error")
|
||||
}
|
||||
|
||||
fn key_to_cert(&self, key: &SecretKey) -> anyhow::Result<Cert> {
|
||||
fn key_to_cert(&self, mut key: SecretKey) -> anyhow::Result<Cert> {
|
||||
let creation_time = SystemTime::now();
|
||||
|
||||
let mut signer = key
|
||||
@@ -174,7 +171,6 @@ impl Fingerprunk {
|
||||
|
||||
// Create certificate
|
||||
let mut cert = Cert::try_from(Packet::SecretKey({
|
||||
let mut key = key.clone();
|
||||
if let Some(ref password) = self.config.password {
|
||||
let (k, mut secret) = key.take_secret();
|
||||
secret.encrypt_in_place(&k, password)?;
|
||||
@@ -204,7 +200,7 @@ impl Fingerprunk {
|
||||
Ok(cert)
|
||||
}
|
||||
|
||||
fn serialize_cert(&self, cert: Cert, to: impl io::Write) -> anyhow::Result<()> {
|
||||
fn serialize_cert(&self, cert: &Cert, to: impl io::Write) -> anyhow::Result<()> {
|
||||
let mut comments = cert.armor_headers();
|
||||
comments.push(format!(
|
||||
"Generated with Fingerprunk. Regex: {}",
|
||||
|
||||
+26
-11
@@ -1,9 +1,9 @@
|
||||
use std::{
|
||||
io::{self, IsTerminal},
|
||||
num::NonZeroU64,
|
||||
num::NonZero,
|
||||
};
|
||||
|
||||
use anyhow::{Context as AnyhowContext, anyhow};
|
||||
use anyhow::{Context, anyhow};
|
||||
use clap::{ArgAction, Parser, ValueEnum};
|
||||
use fancy_regex::Regex;
|
||||
use fingerprunk::Fingerprunk;
|
||||
@@ -31,8 +31,8 @@ struct Args {
|
||||
status: StatusEnabled,
|
||||
|
||||
/// Stop once the specified number of matching keys has been found.
|
||||
#[arg(long)]
|
||||
stop_after: Option<NonZeroU64>,
|
||||
#[arg(long, value_name = "NUM")]
|
||||
stop_after: Option<NonZero<u64>>,
|
||||
|
||||
/// Prompt for a password and use it to encrypt matching keys.
|
||||
///
|
||||
@@ -50,6 +50,13 @@ struct Args {
|
||||
/// Disables the warning about importing keys without user IDs into GnuPG.
|
||||
#[arg(long, conflicts_with = "userid", action = ArgAction::SetTrue)]
|
||||
no_userid: bool,
|
||||
|
||||
/// Use the specified amount of worker threads.
|
||||
///
|
||||
/// If not specified, the amount of worker threads will be set to the amount of the machine's
|
||||
/// available parallelism.
|
||||
#[arg(long, value_name = "NUM")]
|
||||
workers: Option<NonZero<usize>>,
|
||||
}
|
||||
|
||||
#[derive(ValueEnum, Clone, Copy, Debug, Default)]
|
||||
@@ -73,24 +80,33 @@ impl StatusEnabled {
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let workers = match args.workers {
|
||||
Some(workers) => workers,
|
||||
None => std::thread::available_parallelism().context(
|
||||
"unable to determine available parallelism, \
|
||||
use `--workers <NUM>` to specify amount of worker threads",
|
||||
)?,
|
||||
};
|
||||
|
||||
if !args.no_userid && args.userid.is_empty() {
|
||||
eprintln!(
|
||||
"WARNING: No user ID was provided.\n\
|
||||
You may experience problems importing generated keys into GnuPG.\n\
|
||||
Use the --userid option to add a user ID.\n"
|
||||
)
|
||||
Use `--userid <USERID>` to add a user ID.\n"
|
||||
);
|
||||
}
|
||||
|
||||
let password = if args.password {
|
||||
let password = rpassword::prompt_password(
|
||||
"Enter password for encrypting found keys (leave empty for no encryption): ",
|
||||
)
|
||||
.with_context(|| "Failed to prompt password")?;
|
||||
.context("Failed to prompt password")?;
|
||||
|
||||
if password.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let password_retype = rpassword::prompt_password("Retype password: ")
|
||||
.with_context(|| "Failed to prompt password retype")?;
|
||||
.context("Failed to prompt password retype")?;
|
||||
if password_retype == password {
|
||||
Some(password.into())
|
||||
} else {
|
||||
@@ -107,9 +123,8 @@ fn main() -> anyhow::Result<()> {
|
||||
stop_after: args.stop_after,
|
||||
password,
|
||||
userids: args.userid,
|
||||
workers,
|
||||
};
|
||||
|
||||
Fingerprunk::new_from_config(config).run()?;
|
||||
|
||||
Ok(())
|
||||
Fingerprunk::new_from_config(config).run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user