Add a dedicated length-prefixing method to Hasher
This accomplishes two main goals: - Make it clear who is responsible for prefix-freedom, including how they should do it - Make it feasible for a `Hasher` that *doesn't* care about Hash-DoS resistance to get better performance by not hashing lengths This does not change rustc-hash, since that's in an external crate, but that could potentially use it in future.
This commit is contained in:
parent
086bf7a8ff
commit
98054377ee
14 changed files with 166 additions and 6 deletions
|
@ -462,6 +462,14 @@ impl Hasher for SipHasher128 {
|
|||
self.slice_write(msg);
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_str(&mut self, s: &str) {
|
||||
// This hasher works byte-wise, and `0xFF` cannot show up in a `str`,
|
||||
// so just hashing the one extra byte is enough to be prefix-free.
|
||||
self.write(s.as_bytes());
|
||||
self.write_u8(0xFF);
|
||||
}
|
||||
|
||||
fn finish(&self) -> u64 {
|
||||
panic!("SipHasher128 cannot provide valid 64 bit hashes")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue