From 649c69feb9ebc648c08af474770bfe2e2c527fb4 Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Fri, 1 Sep 2023 17:15:54 +0200 Subject: [PATCH] Cleanup enc string display (#214) --- crates/bitwarden/src/crypto/enc_string.rs | 60 +++++++---------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/crates/bitwarden/src/crypto/enc_string.rs b/crates/bitwarden/src/crypto/enc_string.rs index 571806846..57958af8c 100644 --- a/crates/bitwarden/src/crypto/enc_string.rs +++ b/crates/bitwarden/src/crypto/enc_string.rs @@ -184,48 +184,22 @@ impl EncString { impl Display for EncString { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}.", self.enc_type())?; - - let mut parts = Vec::<&[u8]>::new(); - - match self { - EncString::AesCbc256_B64 { iv, data } => { - parts.push(iv); - parts.push(data); - } - EncString::AesCbc128_HmacSha256_B64 { iv, mac, data } => { - parts.push(iv); - parts.push(data); - parts.push(mac); - } - EncString::AesCbc256_HmacSha256_B64 { iv, mac, data } => { - parts.push(iv); - parts.push(data); - parts.push(mac); - } - EncString::Rsa2048_OaepSha256_B64 { data } => { - parts.push(data); - } - EncString::Rsa2048_OaepSha1_B64 { data } => { - parts.push(data); - } - EncString::Rsa2048_OaepSha256_HmacSha256_B64 { mac, data } => { - parts.push(data); - parts.push(mac); - } - EncString::Rsa2048_OaepSha1_HmacSha256_B64 { mac, data } => { - parts.push(data); - parts.push(mac); - } - } - - for i in 0..parts.len() { - if i == parts.len() - 1 { - write!(f, "{}", BASE64_ENGINE.encode(parts[i]))?; - } else { - write!(f, "{}|", BASE64_ENGINE.encode(parts[i]))?; - } - } + let parts: Vec<&[u8]> = match self { + EncString::AesCbc256_B64 { iv, data } => vec![iv, data], + EncString::AesCbc128_HmacSha256_B64 { iv, mac, data } => vec![iv, data, mac], + EncString::AesCbc256_HmacSha256_B64 { iv, mac, data } => vec![iv, data, mac], + EncString::Rsa2048_OaepSha256_B64 { data } => vec![data], + EncString::Rsa2048_OaepSha1_B64 { data } => vec![data], + EncString::Rsa2048_OaepSha256_HmacSha256_B64 { mac, data } => vec![data, mac], + EncString::Rsa2048_OaepSha1_HmacSha256_B64 { mac, data } => vec![data, mac], + }; + + let encoded_parts: Vec = parts + .iter() + .map(|part| BASE64_ENGINE.encode(part)) + .collect(); + + write!(f, "{}.{}", self.enc_type(), encoded_parts.join("|"))?; Ok(()) } @@ -241,7 +215,7 @@ impl<'de> Deserialize<'de> for EncString { type Value = EncString; fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "A valid string") + write!(f, "a valid string") } fn visit_str(self, v: &str) -> Result