diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-07-15 18:04:20 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-07-15 18:05:05 +0200 |
commit | 0a56e0e8080da97a761fcdda0bae4e72a39b3d66 (patch) | |
tree | 917d429da428ab265817dd150350c7a42ad9d5a5 /openpgp/examples | |
parent | 84ee468b1d27d2b8d49447a5fbfc95d8f6e2e7b1 (diff) |
openpgp: Collect statistics about public key algorithms.
Diffstat (limited to 'openpgp/examples')
-rw-r--r-- | openpgp/examples/statistics.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/openpgp/examples/statistics.rs b/openpgp/examples/statistics.rs index 1dfebe38..cd2267ba 100644 --- a/openpgp/examples/statistics.rs +++ b/openpgp/examples/statistics.rs @@ -78,6 +78,10 @@ fn main() { let mut ua_unknown_count = vec![0; 256]; let mut ua_invalid_count = 0; + // Key statistics. + let mut pk_algo_size: HashMap<PublicKeyAlgorithm, HashMap<usize, usize>> = + Default::default(); + // Current certificate. let mut current_fingerprint = KeyHandle::Fingerprint(Fingerprint::from_bytes(&vec![0; 20])); @@ -245,6 +249,35 @@ fn main() { _ => (), } + // Public key algorithm and size statistics. + let mut handle_key = |k: &openpgp::packet::Key<_, _>| { + let pk = k.pk_algo(); + let bits = k.mpis().bits().unwrap_or(0); + if let Some(size_hash) = pk_algo_size.get_mut(&pk) { + if let Some(count) = size_hash.get_mut(&bits) { + *count = *count + 1; + } else { + size_hash.insert(bits, 1); + } + } else { + let mut size_hash: HashMap<usize, usize> + = Default::default(); + size_hash.insert(bits, 1); + pk_algo_size.insert(pk, size_hash); + } + }; + match packet { + Packet::PublicKey(ref k) => + handle_key(k.parts_as_public().role_as_unspecified()), + Packet::SecretKey(ref k) => + handle_key(k.parts_as_public().role_as_unspecified()), + Packet::PublicSubkey(ref k) => + handle_key(k.parts_as_public().role_as_unspecified()), + Packet::SecretSubkey(ref k) => + handle_key(k.parts_as_public().role_as_unspecified()), + _ => (), + } + if let Packet::Unknown(_) = packet { tags_unknown[i] += 1; } else { @@ -516,6 +549,22 @@ fn main() { } println!(); + println!("# Key statistics\n\n\ + {:>50} {:>9} {:>9}", + "Algorithm", "Key Size", "count"); + println!("----------------------------------------------------------------------"); + for t in 0..255u8 { + let pk = PublicKeyAlgorithm::from(t); + if let Some(size_hash) = pk_algo_size.get(&pk) { + let mut sizes: Vec<_> = size_hash.iter().collect(); + sizes.sort_by_key(|(size, _count)| *size); + for (size, count) in sizes { + println!("{:>50} {:>9} {:>9}", pk.to_string(), size, count); + } + } + } + + println!(); println!("# Cert statistics\n\n\ {:>30} {:>9} {:>9} {:>9}", "", "min", "mean", "max"); |