From b1b640ae8bd8d2ed7c128cb3f90e3e56226a5560 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 21 Feb 2021 15:24:30 +0100 Subject: [PATCH] rename hash() to sha256sum(), we might want to change it at some places --- src/api/s3_put.rs | 30 +++++++++++++++--------------- src/api/signature.rs | 4 ++-- src/model/block.rs | 2 +- src/rpc/membership.rs | 10 ++++------ src/rpc/ring.rs | 2 +- src/table/schema.rs | 2 +- src/table/table_fullcopy.rs | 2 +- src/table/table_sync.rs | 8 ++++---- src/util/data.rs | 2 +- 9 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/api/s3_put.rs b/src/api/s3_put.rs index 4a2af91..969b409 100644 --- a/src/api/s3_put.rs +++ b/src/api/s3_put.rs @@ -55,7 +55,7 @@ pub async fn handle_put( let md5sum_arr = md5sum.finalize(); let md5sum_hex = hex::encode(md5sum_arr); - let sha256sum_hash = hash(&first_block[..]); + let sha256sum_hash = sha256sum(&first_block[..]); ensure_checksum_matches( md5sum_arr.as_slice(), @@ -95,7 +95,7 @@ pub async fn handle_put( // Initialize corresponding entry in version table let version = Version::new(version_uuid, bucket.into(), key.into(), false, vec![]); - let first_block_hash = hash(&first_block[..]); + let first_block_hash = sha256sum(&first_block[..]); // Transfer data and verify checksum let tx_result = read_and_put_blocks( @@ -180,10 +180,10 @@ async fn read_and_put_blocks( first_block_hash: Hash, chunker: &mut BodyChunker, ) -> Result<(u64, GenericArray, Hash), Error> { - let mut md5sum = Md5::new(); - let mut sha256sum = Sha256::new(); - md5sum.update(&first_block[..]); - sha256sum.input(&first_block[..]); + let mut md5hasher = Md5::new(); + let mut sha256hasher = Sha256::new(); + md5hasher.update(&first_block[..]); + sha256hasher.input(&first_block[..]); let mut next_offset = first_block.len(); let mut put_curr_version_block = put_block_meta( @@ -202,9 +202,9 @@ async fn read_and_put_blocks( let (_, _, next_block) = futures::try_join!(put_curr_block, put_curr_version_block, chunker.next())?; if let Some(block) = next_block { - md5sum.update(&block[..]); - sha256sum.input(&block[..]); - let block_hash = hash(&block[..]); + md5hasher.update(&block[..]); + sha256hasher.input(&block[..]); + let block_hash = sha256sum(&block[..]); let block_len = block.len(); put_curr_version_block = put_block_meta( garage.clone(), @@ -222,14 +222,14 @@ async fn read_and_put_blocks( } let total_size = next_offset as u64; - let md5sum_arr = md5sum.finalize(); + let md5sum_arr = md5hasher.finalize(); - let sha256sum = sha256sum.result(); + let sha256sum_arr = sha256hasher.result(); let mut hash = [0u8; 32]; - hash.copy_from_slice(&sha256sum[..]); - let sha256sum = Hash::from(hash); + hash.copy_from_slice(&sha256sum_arr[..]); + let sha256sum_arr = Hash::from(hash); - Ok((total_size, md5sum_arr, sha256sum)) + Ok((total_size, md5sum_arr, sha256sum_arr)) } async fn put_block_meta( @@ -390,7 +390,7 @@ pub async fn handle_put_part( // Copy block to store let version = Version::new(version_uuid, bucket, key, false, vec![]); - let first_block_hash = hash(&first_block[..]); + let first_block_hash = sha256sum(&first_block[..]); let (_, md5sum_arr, sha256sum) = read_and_put_blocks( &garage, version, diff --git a/src/api/signature.rs b/src/api/signature.rs index a987646..b635063 100644 --- a/src/api/signature.rs +++ b/src/api/signature.rs @@ -6,7 +6,7 @@ use hyper::{Body, Method, Request}; use sha2::{Digest, Sha256}; use garage_table::*; -use garage_util::data::{hash, Hash}; +use garage_util::data::{sha256sum, Hash}; use garage_model::garage::Garage; use garage_model::key_table::*; @@ -296,7 +296,7 @@ fn canonical_query_string(uri: &hyper::Uri) -> String { pub fn verify_signed_content(content_sha256: Option, body: &[u8]) -> Result<(), Error> { let expected_sha256 = content_sha256.ok_or_bad_request("Request content hash not signed, aborting.")?; - if expected_sha256 != hash(body) { + if expected_sha256 != sha256sum(body) { return Err(Error::BadRequest(format!("Request content hash does not match signed hash"))); } Ok(()) diff --git a/src/model/block.rs b/src/model/block.rs index 8a513a3..1627ef8 100644 --- a/src/model/block.rs +++ b/src/model/block.rs @@ -176,7 +176,7 @@ impl BlockManager { f.read_to_end(&mut data).await?; drop(f); - if data::hash(&data[..]) != *hash { + if data::sha256sum(&data[..]) != *hash { let _lock = self.data_dir_lock.lock().await; warn!("Block {:?} is corrupted. Deleting and resyncing.", hash); fs::remove_file(path).await?; diff --git a/src/rpc/membership.rs b/src/rpc/membership.rs index 4872899..2d3b37a 100644 --- a/src/rpc/membership.rs +++ b/src/rpc/membership.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::io::{Read, Write}; +use std::fmt::Write as FmtWrite; use std::net::{IpAddr, SocketAddr}; use std::path::PathBuf; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -10,7 +11,6 @@ use futures::future::join_all; use futures::select; use futures_util::future::*; use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; use tokio::prelude::*; use tokio::sync::watch; use tokio::sync::Mutex; @@ -134,16 +134,14 @@ impl Status { let mut nodes = self.nodes.iter().collect::>(); nodes.sort_unstable_by_key(|(id, _status)| *id); - let mut hasher = Sha256::new(); + let mut nodes_txt = String::new(); debug!("Current set of pingable nodes: --"); for (id, status) in nodes { debug!("{} {}", hex::encode(&id), status.addr); - hasher.input(format!("{} {}\n", hex::encode(&id), status.addr)); + writeln!(&mut nodes_txt, "{} {}", hex::encode(&id), status.addr).unwrap(); } debug!("END --"); - self.hash - .as_slice_mut() - .copy_from_slice(&hasher.result()[..]); + self.hash = sha256sum(nodes_txt.as_bytes()); } } diff --git a/src/rpc/ring.rs b/src/rpc/ring.rs index 1646afb..1df0bb4 100644 --- a/src/rpc/ring.rs +++ b/src/rpc/ring.rs @@ -56,7 +56,7 @@ impl Ring { let datacenter_idx = datacenters.iter().enumerate().find(|(_, dc)| *dc == datacenter).unwrap().0; for i in 0..config.n_tokens { - let location = hash(format!("{} {}", hex::encode(&id), i).as_bytes()); + let location = sha256sum(format!("{} {}", hex::encode(&id), i).as_bytes()); new_ring.push(RingEntry { location: location.into(), diff --git a/src/table/schema.rs b/src/table/schema.rs index d2ec945..61deb3c 100644 --- a/src/table/schema.rs +++ b/src/table/schema.rs @@ -10,7 +10,7 @@ pub trait PartitionKey { impl PartitionKey for String { fn hash(&self) -> Hash { - hash(self.as_bytes()) + sha256sum(self.as_bytes()) } } diff --git a/src/table/table_fullcopy.rs b/src/table/table_fullcopy.rs index 4659cb2..e6f2c5f 100644 --- a/src/table/table_fullcopy.rs +++ b/src/table/table_fullcopy.rs @@ -44,7 +44,7 @@ impl TableFullReplication { let mut nodes = vec![]; for (node, _) in ring.config.members.iter() { - let node_ranking = hash(&[node.as_slice(), my_id.as_slice()].concat()); + let node_ranking = sha256sum(&[node.as_slice(), my_id.as_slice()].concat()); nodes.push((*node, node_ranking)); } nodes.sort_by(|(_, rank1), (_, rank2)| rank1.cmp(rank2)); diff --git a/src/table/table_sync.rs b/src/table/table_sync.rs index 3c66798..86878e3 100644 --- a/src/table/table_sync.rs +++ b/src/table/table_sync.rs @@ -322,7 +322,7 @@ where .range(range.begin.clone()..range.end.clone()) { let (key, value) = item?; - let key_hash = hash(&key[..]); + let key_hash = sha256sum(&key[..]); if children.len() > 0 && key_hash.as_slice()[0..range.level] .iter() @@ -340,7 +340,7 @@ where end: vec![], level: 0, }; - children.push((item_range, hash(&value[..]))); + children.push((item_range, sha256sum(&value[..]))); } Ok(RangeChecksum { bounds: range.clone(), @@ -378,7 +378,7 @@ where } let found_limit = sub_ck.found_limit.unwrap(); - let actual_limit_hash = hash(&found_limit[..]); + let actual_limit_hash = sha256sum(&found_limit[..]); if actual_limit_hash.as_slice()[0..range.level] .iter() .all(|x| *x == 0u8) @@ -426,7 +426,7 @@ where ); let hash = if v.children.len() > 0 { - Some(hash(&rmp_to_vec_all_named(&v)?[..])) + Some(sha256sum(&rmp_to_vec_all_named(&v)?[..])) } else { None }; diff --git a/src/util/data.rs b/src/util/data.rs index 8f976f7..657467b 100644 --- a/src/util/data.rs +++ b/src/util/data.rs @@ -77,7 +77,7 @@ impl FixedBytes32 { pub type UUID = FixedBytes32; pub type Hash = FixedBytes32; -pub fn hash(data: &[u8]) -> Hash { +pub fn sha256sum(data: &[u8]) -> Hash { let mut hasher = Sha256::new(); hasher.input(data); let mut hash = [0u8; 32];