Skip to content

Commit d938275

Browse files
authored
assets: use blake3 instead of md5 (#10208)
# Objective - Replace md5 by another hasher, as suggested in #8624 (comment) - md5 is not secure, and is slow. use something more secure and faster ## Solution - Replace md5 by blake3 Putting this PR in the 0.12 as once it's released, changing the hash algorithm will be a painful breaking change
1 parent e59085a commit d938275

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

crates/bevy_asset/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ crossbeam-channel = "0.5"
3333
downcast-rs = "1.2"
3434
futures-io = "0.3"
3535
futures-lite = "1.12"
36-
md5 = "0.7"
36+
blake3 = "1.5"
3737
parking_lot = { version = "0.12", features = ["arc_lock", "send_guard"] }
3838
ron = "0.8"
3939
serde = { version = "1", features = ["derive"] }

crates/bevy_asset/src/meta.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,27 +225,25 @@ pub(crate) fn loader_settings_meta_transform<S: Settings>(
225225
})
226226
}
227227

228-
pub type AssetHash = [u8; 16];
228+
pub type AssetHash = [u8; 32];
229229

230230
/// NOTE: changing the hashing logic here is a _breaking change_ that requires a [`META_FORMAT_VERSION`] bump.
231231
pub(crate) fn get_asset_hash(meta_bytes: &[u8], asset_bytes: &[u8]) -> AssetHash {
232-
let mut context = md5::Context::new();
233-
context.consume(meta_bytes);
234-
context.consume(asset_bytes);
235-
let digest = context.compute();
236-
digest.0
232+
let mut hasher = blake3::Hasher::new();
233+
hasher.update(meta_bytes);
234+
hasher.update(asset_bytes);
235+
*hasher.finalize().as_bytes()
237236
}
238237

239238
/// NOTE: changing the hashing logic here is a _breaking change_ that requires a [`META_FORMAT_VERSION`] bump.
240239
pub(crate) fn get_full_asset_hash(
241240
asset_hash: AssetHash,
242241
dependency_hashes: impl Iterator<Item = AssetHash>,
243242
) -> AssetHash {
244-
let mut context = md5::Context::new();
245-
context.consume(asset_hash);
243+
let mut hasher = blake3::Hasher::new();
244+
hasher.update(&asset_hash);
246245
for hash in dependency_hashes {
247-
context.consume(hash);
246+
hasher.update(&hash);
248247
}
249-
let digest = context.compute();
250-
digest.0
248+
*hasher.finalize().as_bytes()
251249
}

0 commit comments

Comments
 (0)