[pbs-devel] [PATCH proxmox-backup 02/13] key: add fingerprint to key config

Wolfgang Bumiller w.bumiller at proxmox.com
Mon Nov 23 09:07:03 CET 2020


On Fri, Nov 20, 2020 at 05:38:32PM +0100, Fabian Grünbichler wrote:
> diff --git a/src/tools/format.rs b/src/tools/format.rs
> index 8fe6aa82..c9f50053 100644
> --- a/src/tools/format.rs
> +++ b/src/tools/format.rs
> @@ -102,6 +102,40 @@ impl From<u64> for HumanByte {
>      }
>  }
>  

Minor nit (maybe for a follow-up cleanup depending on how the rest of
the series goes):

> +pub fn as_fingerprint(bytes: &[u8]) -> String {
> +    proxmox::tools::digest_to_hex(bytes)
> +        .as_bytes()
> +        .chunks(2)
> +        .map(|v| std::str::from_utf8(v).unwrap())

^ this performs an unnecessary check, even if you just unwrap it, use

    unsafe { std::str::from_utf8_unchecked(v) }

> +        .collect::<Vec<&str>>().join(":")

^ this allocates multiple tiny vectors.

Since you already know even the length of the resulting string it's
nicer to just allocate it before hand and fill it in a loop.

    lex hex = proxmox::tools::digest_to_hex(bytes);

    // avoid underflow:
    if hex.is_empty() {
        return String::new();
    }

    let mut out = String::with_capacity((hex.len() / 2) - 1);
    for pair in hex.as_bytes().chunks(2) {
        if !out.is_empty() {
            out.push(':');
        }
        out.push_str(unsafe { std::str::from_utf8_unchecked(pair) });
    }
    out





More information about the pbs-devel mailing list