add armor comment to output certificates

This commit is contained in:
2025-10-23 23:44:14 +02:00
parent bbc17385a7
commit 6620aa898d
+29 -7
View File
@@ -14,7 +14,7 @@ use std::{
use fancy_regex::Regex; use fancy_regex::Regex;
use num_integer::Integer; use num_integer::Integer;
use sequoia_openpgp::{ use sequoia_openpgp::{
Cert, Packet, Cert, Packet, armor,
crypto::Password, crypto::Password,
packet::{ packet::{
Key, Key,
@@ -117,17 +117,16 @@ impl Fingerprunk {
let mut stdout = io::stdout().lock(); let mut stdout = io::stdout().lock();
for key in matches_rx { for key in matches_rx {
let result = self let cert = self
.key_to_cert(&key) .key_to_cert(&key)
.and_then(|cert| cert.as_tsk().armored().serialize(&mut stdout)); .expect("should be able to create certificate");
self.serialize_cert(cert, &mut stdout)
.expect("should be able to serialize certificate");
if let Err(err) = result {
eprintln!("Error: {err}");
} else {
self.counter_found.fetch_add(1, Ordering::Relaxed); self.counter_found.fetch_add(1, Ordering::Relaxed);
} }
} }
}
fn key_to_cert(&self, key: &SecretKey) -> anyhow::Result<Cert> { fn key_to_cert(&self, key: &SecretKey) -> anyhow::Result<Cert> {
let sig = SignatureBuilder::new(SignatureType::DirectKey) let sig = SignatureBuilder::new(SignatureType::DirectKey)
@@ -157,6 +156,29 @@ impl Fingerprunk {
Cert::try_from(vec![secret_key_packet, Packet::from(sig)]) Cert::try_from(vec![secret_key_packet, Packet::from(sig)])
} }
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: {}",
self.config.regex
));
let headers: Vec<_> = comments
.into_iter()
.map(|s| ("Comment".to_string(), s))
.collect();
let mut writer = armor::Writer::with_headers(to, armor::Kind::SecretKey, headers)?;
// Set the profile to RFC4880 because we generate v4 keys.
writer.set_profile(sequoia_openpgp::Profile::RFC4880)?;
cert.serialize(&mut writer)?;
writer.finalize()?;
Ok(())
}
fn status_displayer_thread(&self) { fn status_displayer_thread(&self) {
struct DurationDhms(Duration); struct DurationDhms(Duration);