2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-07-12 22:58:39 +00:00
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
use std::hash::Hasher;
|
|
|
|
|
|
|
|
/// A very fast insecure hasher that uses the xxHash algorithm.
|
|
|
|
pub struct FastInsecureHasher(twox_hash::XxHash64);
|
|
|
|
|
|
|
|
impl FastInsecureHasher {
|
2024-05-29 18:38:18 +00:00
|
|
|
pub fn new_without_deno_version() -> Self {
|
|
|
|
Self(Default::default())
|
2022-07-19 15:58:18 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 18:38:18 +00:00
|
|
|
pub fn new_deno_versioned() -> Self {
|
|
|
|
let mut hasher = Self::new_without_deno_version();
|
2024-08-15 21:47:16 +00:00
|
|
|
hasher.write_str(crate::version::DENO_VERSION_INFO.deno);
|
2024-05-29 18:38:18 +00:00
|
|
|
hasher
|
2023-07-10 21:45:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
pub fn write_str(&mut self, text: &str) -> &mut Self {
|
|
|
|
self.write(text.as_bytes());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(&mut self, bytes: &[u8]) -> &mut Self {
|
|
|
|
self.0.write(bytes);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-30 15:43:03 +00:00
|
|
|
pub fn write_u8(&mut self, value: u8) -> &mut Self {
|
|
|
|
self.0.write_u8(value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
pub fn write_u64(&mut self, value: u64) -> &mut Self {
|
|
|
|
self.0.write_u64(value);
|
|
|
|
self
|
|
|
|
}
|
2022-07-12 22:58:39 +00:00
|
|
|
|
2022-09-07 19:06:18 +00:00
|
|
|
pub fn write_hashable(
|
|
|
|
&mut self,
|
2023-07-10 21:45:09 +00:00
|
|
|
hashable: impl std::hash::Hash,
|
2022-09-07 19:06:18 +00:00
|
|
|
) -> &mut Self {
|
|
|
|
hashable.hash(&mut self.0);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-07-19 15:58:18 +00:00
|
|
|
pub fn finish(&self) -> u64 {
|
|
|
|
self.0.finish()
|
|
|
|
}
|
2022-07-12 22:58:39 +00:00
|
|
|
}
|