Skip to content

Commit eb8fbdc

Browse files
committed
chore(hermes): update code so that it uses 4 byte hash
1 parent a4f38c2 commit eb8fbdc

File tree

1 file changed

+6
-5
lines changed
  • hermes/bin/src/runtime_extensions/hermes/doc_sync

1 file changed

+6
-5
lines changed

hermes/bin/src/runtime_extensions/hermes/doc_sync/host.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ impl HostSyncChannel for HermesRuntimeContext {
9494
&mut self,
9595
name: ChannelName,
9696
) -> wasmtime::Result<Resource<SyncChannel>> {
97-
let hash = blake2b_simd::blake2b(name.as_bytes());
97+
let hash = blake2b_simd::Params::new()
98+
.hash_length(4)
99+
.hash(name.as_bytes());
98100

99101
// The digest is a 64-byte array ([u8; 64]) for 512-bit output.
100102
// Take the first 4 bytes to use them as resource id.
@@ -104,12 +106,11 @@ impl HostSyncChannel for HermesRuntimeContext {
104106
// acceptable but unlikely in practice. We use the first 4 bytes of
105107
// the cryptographically secure Blake2b hash as a fast, 32-bit ID
106108
// to minimize lock contention when accessing state via DOC_SYNC_STATE.
107-
#[allow(clippy::indexing_slicing)]
108-
let prefix_bytes: [u8; 4] = hash.as_bytes()[..4].try_into().map_err(|err| {
109-
wasmtime::Error::msg(format!("error trimming channel name hash: {err}"))
109+
let prefix_bytes: &[u8; 4] = hash.as_bytes().try_into().map_err(|err| {
110+
wasmtime::Error::msg(format!("BLAKE2b hash output length must be 4 bytes: {err}"))
110111
})?;
111112

112-
let resource: u32 = u32::from_be_bytes(prefix_bytes);
113+
let resource: u32 = u32::from_be_bytes(*prefix_bytes);
113114

114115
// Code block is used to minimize locking scope.
115116
{

0 commit comments

Comments
 (0)