From d738939f898d5f3ff6335be22d6868cb30e963b7 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 15 Dec 2024 17:30:19 +0100 Subject: [PATCH] xmpp-parsers: Simplify hash conversion to Vec Use `GenericArray::to_vec()` and `slice::to_vec()` directly. --- parsers/src/caps.rs | 31 ++++++++++++------------------- parsers/src/ecaps2.rs | 14 ++++---------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/parsers/src/caps.rs b/parsers/src/caps.rs index d21dea82f0abb81911cc417f3258c32781310b91..f9f6ba1ce547dbad854413df0538ff100d5fa163 100644 --- a/parsers/src/caps.rs +++ b/parsers/src/caps.rs @@ -110,9 +110,7 @@ fn compute_identities(identities: &[Identity]) -> Vec { let lang = identity.lang.clone().unwrap_or_default(); let name = identity.name.clone().unwrap_or_default(); let string = format!("{}/{}/{}/{}", identity.category, identity.type_, lang, name); - let bytes = string.as_bytes(); - let mut vec = Vec::with_capacity(bytes.len()); - vec.extend_from_slice(bytes); + let mut vec = string.as_bytes().to_vec(); vec.push(b'<'); vec }) @@ -120,11 +118,12 @@ fn compute_identities(identities: &[Identity]) -> Vec { fn compute_extensions(extensions: &[DataForm]) -> Vec { compute_items(extensions, |extension| { - let mut bytes = vec![]; // TODO: maybe handle the error case? - if let Some(ref form_type) = extension.form_type { - bytes.extend_from_slice(form_type.as_bytes()); - } + let mut bytes = if let Some(ref form_type) = extension.form_type { + form_type.as_bytes().to_vec() + } else { + vec![] + }; bytes.push(b'<'); for field in extension.fields.clone() { if field.var.as_deref() == Some("FORM_TYPE") { @@ -159,10 +158,6 @@ pub fn compute_disco(disco: &DiscoInfoResult) -> Vec { final_string } -fn get_hash_vec(hash: &[u8]) -> Vec { - hash.to_vec() -} - /// Hashes the result of [compute_disco()] with one of the supported [hash /// algorithms](../hashes/enum.Algo.html). pub fn hash_caps(data: &[u8], algo: Algo) -> Result { @@ -170,23 +165,23 @@ pub fn hash_caps(data: &[u8], algo: Algo) -> Result { hash: match algo { Algo::Sha_1 => { let hash = Sha1::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha_256 => { let hash = Sha256::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha_512 => { let hash = Sha512::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha3_256 => { let hash = Sha3_256::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha3_512 => { let hash = Sha3_512::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Blake2b_256 => { let mut hasher = Blake2bVar::new(32).unwrap(); @@ -281,9 +276,7 @@ mod tests { .parse() .unwrap(); - let data = b"client/pc//Exodus 0.9.1 Result, Error> { Ok(final_string) } -fn get_hash_vec(hash: &[u8]) -> Vec { - let mut vec = Vec::with_capacity(hash.len()); - vec.extend_from_slice(hash); - vec -} - /// Hashes the result of [compute_disco()] with one of the supported [hash /// algorithms](../hashes/enum.Algo.html). pub fn hash_ecaps2(data: &[u8], algo: Algo) -> Result { @@ -137,19 +131,19 @@ pub fn hash_ecaps2(data: &[u8], algo: Algo) -> Result { hash: match algo { Algo::Sha_256 => { let hash = Sha256::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha_512 => { let hash = Sha512::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha3_256 => { let hash = Sha3_256::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Sha3_512 => { let hash = Sha3_512::digest(data); - get_hash_vec(hash.as_slice()) + hash.to_vec() } Algo::Blake2b_256 => { let mut hasher = Blake2bVar::new(32).unwrap();