From 8c1ec5c8021b11d4ef657a55c67b060045e6ebdc Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 17 Nov 2021 07:07:40 +0100 Subject: [PATCH] move fingerprint helpers from pbs-tools to pbs-api-types Signed-off-by: Dietmar Maurer --- pbs-api-types/Cargo.toml | 2 +- pbs-api-types/src/crypto.rs | 42 +++++++++++++++++++++++++++++++++++-- pbs-api-types/src/lib.rs | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pbs-api-types/Cargo.toml b/pbs-api-types/Cargo.toml index 840d36bd..585bb9c6 100644 --- a/pbs-api-types/Cargo.toml +++ b/pbs-api-types/Cargo.toml @@ -7,6 +7,7 @@ description = "general API type helpers for PBS" [dependencies] anyhow = "1.0" +hex = "0.4.3" lazy_static = "1.4" libc = "0.2" nix = "0.19.1" @@ -21,4 +22,3 @@ proxmox-time = "1.0.0" proxmox-uuid = { version = "1.0.0", features = [ "serde" ] } proxmox-systemd = { path = "../proxmox-systemd" } -pbs-tools = { path = "../pbs-tools" } diff --git a/pbs-api-types/src/crypto.rs b/pbs-api-types/src/crypto.rs index 016970f8..eda92e23 100644 --- a/pbs-api-types/src/crypto.rs +++ b/pbs-api-types/src/crypto.rs @@ -5,8 +5,6 @@ use serde::{Deserialize, Serialize}; use proxmox_schema::api; -use pbs_tools::format::{as_fingerprint, bytes_as_fingerprint}; - #[api(default: "encrypt")] #[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] @@ -55,3 +53,43 @@ impl std::str::FromStr for Fingerprint { } } +fn as_fingerprint(bytes: &[u8]) -> String { + hex::encode(bytes) + .as_bytes() + .chunks(2) + .map(|v| unsafe { std::str::from_utf8_unchecked(v) }) // it's a hex string + .collect::>().join(":") +} + +pub mod bytes_as_fingerprint { + use std::mem::MaybeUninit; + + use serde::{Deserialize, Serializer, Deserializer}; + + pub fn serialize( + bytes: &[u8; 32], + serializer: S, + ) -> Result + where + S: Serializer, + { + let s = super::as_fingerprint(bytes); + serializer.serialize_str(&s) + } + + pub fn deserialize<'de, D>( + deserializer: D, + ) -> Result<[u8; 32], D::Error> + where + D: Deserializer<'de>, + { + // TODO: more efficiently implement with a Visitor implementing visit_str using split() and + // hex::decode by-byte + let mut s = String::deserialize(deserializer)?; + s.retain(|c| c != ':'); + let mut out = MaybeUninit::<[u8; 32]>::uninit(); + hex::decode_to_slice(s.as_bytes(), unsafe { &mut (*out.as_mut_ptr())[..] }) + .map_err(serde::de::Error::custom)?; + Ok(unsafe { out.assume_init() }) + } +} diff --git a/pbs-api-types/src/lib.rs b/pbs-api-types/src/lib.rs index a61de960..eebf5794 100644 --- a/pbs-api-types/src/lib.rs +++ b/pbs-api-types/src/lib.rs @@ -64,7 +64,7 @@ pub use user::*; pub use proxmox_schema::upid::*; mod crypto; -pub use crypto::{CryptMode, Fingerprint}; +pub use crypto::{CryptMode, Fingerprint, bytes_as_fingerprint}; pub mod file_restore;