From a02139ee79c3617388263249f2eef10d7c40e537 Mon Sep 17 00:00:00 2001 From: Davide Galassi Date: Thu, 15 Sep 2022 17:11:58 +0200 Subject: [PATCH] Replace blake2-rcf with blake2 --- Cargo.toml | 2 +- src/column.rs | 17 ++++++++++++++--- src/table.rs | 4 +++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08a2ed54..7c3bca40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/paritytech/parity-db/" description = "Key-value database for the blockchain" [dependencies] -blake2-rfc = "0.2.18" +blake2 = "0.10.4" crc32fast = "1.2.0" fs2 = "0.4.3" hex = "0.4.2" diff --git a/src/column.rs b/src/column.rs index 5000cf78..34d9bf2e 100644 --- a/src/column.rs +++ b/src/column.rs @@ -125,6 +125,11 @@ enum IterStateOrCorrupted { #[inline] pub fn hash_key(key: &[u8], salt: &Salt, uniform: bool, db_version: u32) -> Key { + use blake2::{ + digest::{typenum::U32, FixedOutput, Update}, + Blake2bMac, + }; + let mut k = Key::default(); if uniform { if db_version <= 5 { @@ -137,7 +142,11 @@ pub fn hash_key(key: &[u8], salt: &Salt, uniform: bool, db_version: u32) -> Key } } } else { - k.copy_from_slice(blake2_rfc::blake2b::blake2b(32, salt, key).as_bytes()); + let mut ctx = Blake2bMac::::new_with_salt_and_personal(salt, &[], &[]) + .expect("Salt length (32) is a valid key length (<= 64)"); + ctx.update(key); + let hash = ctx.finalize_fixed(); + k.copy_from_slice(&hash); } k } @@ -676,6 +685,8 @@ impl HashColumn { start_chunk: u64, skip_preimage_indexes: bool, ) -> Result<()> { + use blake2::{digest::typenum::U32, Blake2b, Digest}; + let tables = self.tables.read(); let source = &tables.index; @@ -694,8 +705,8 @@ impl HashColumn { } else { value }; - let key = blake2_rfc::blake2b::blake2b(32, &[], &value); - let key = self.hash_key(key.as_bytes()); + let key = Blake2b::::digest(&value); + let key = self.hash_key(&key); let state = IterStateOrCorrupted::Item(IterState { chunk_index: index, key, diff --git a/src/table.rs b/src/table.rs index 2e589349..5ac9b2e4 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1147,8 +1147,10 @@ mod test { } fn key(k: u32) -> Key { + use blake2::{digest::typenum::U32, Blake2b, Digest}; let mut key = Key::default(); - key.copy_from_slice(blake2_rfc::blake2b::blake2b(32, &[], &k.to_le_bytes()).as_bytes()); + let hash = Blake2b::::digest(&k.to_le_bytes()); + key.copy_from_slice(&hash); key }