Struct djb_hash::x33a_u32::X33aU32 [−][src]
pub struct X33aU32 { /* fields omitted */ }
Implements 32 bit version of one of the original hash functions post by Daniel J. Bernstein.
Examples
use std::hash::Hasher; use djb_hash::HasherU32; use djb_hash::x33a_u32::*; let input = "Ez"; let mut hasher = X33aU32::new(); hasher.write(&input.as_bytes()); assert_eq!(hasher.finish(), 5862308u64); assert_eq!(hasher.finish_u32(), 5862308u32);
Another example:
let input = "FY"; let mut hasher = X33aU32::new(); hasher.write(&input.as_bytes()); assert_eq!(hasher.finish(), 5862308u64);
These two examples show one of the known issues with this hash function: hash collision. Both strings hash to the same value. If it was being used in a public face HashMap it would open a possible DoS attack vector. Java, JS, Python and PHP have all experience these types of attacks when using this or other similar hash functions.
Adding prefixes and/or suffixes will change the hash but not the collision. An example:
let input1 = "abcEzpie"; let input2 = "abcFYpie"; let mut hasher1 = X33aU32::new(); let mut hasher2 = X33aU32::new(); hasher1.write(&input1.as_bytes()); hasher2.write(&input2.as_bytes()); assert_eq!(hasher1.finish(), 1686394568u64); assert_eq!(hasher1.finish(), hasher2.finish());
Methods
impl X33aU32
[src]
impl X33aU32
pub fn new() -> Self
[src]
pub fn new() -> Self
Creates a new hash using the original 5381 prime number salt value used by DJB.
pub fn new_with_salt(s: u32) -> Self
[src]
pub fn new_with_salt(s: u32) -> Self
Creates a new hash using user supplied salt value.
The supplied salt needs to be a prime number. It should have bits in more than just the lower 8 bits but setting any bits past half the size of the hash is of limited use as they are quickly lost during the multiplication stage for long values and tend to because static for very short values. Primes between 16 to 32 bits for 64 bit hashes seem to work best in most cases and between 16 to 24 bits for 32 bit hashes.
Examples
use std::hash::Hasher; use djb_hash::HasherU32; use djb_hash::x33a_u32::*; let input = "Ez"; let mut hasher = X33aU32::new_with_salt(5381); hasher.write(&input.as_bytes()); assert_eq!(hasher.finish(), 5862308u64); assert_eq!(hasher.finish_u32(), 5862308u32);
Another example:
let input = "FY"; let mut hasher = X33aU32::new_with_salt(5387); hasher.write(&input.as_bytes()); assert_eq!(hasher.finish(), 5868842u64); assert_eq!(hasher.finish_u32(), 5868842u32);
These examples show how the hashes change with different salts but in the next example you can see the same strings, "Ez" and "FY", will still collide.
let input = "Ez"; let mut hasher = X33aU32::new_with_salt(5387); hasher.write(&input.as_bytes()); assert_eq!(hasher.finish(), 5868842u64);
Trait Implementations
impl HasherU32 for X33aU32
[src]
impl HasherU32 for X33aU32
fn finish_u32(&self) -> u32
[src]
fn finish_u32(&self) -> u32
Examples
let input = "FY"; let mut hasher = X33a::new(); hasher.write(&input.as_bytes()); assert_eq!(hasher.finish(), 5862308u64);
impl Hasher for X33aU32
[src]
impl Hasher for X33aU32
fn finish(&self) -> u64
[src]
fn finish(&self) -> u64
Returns the hash value for the values written so far. Read more
fn write(&mut self, bytes: &[u8])
[src]
fn write(&mut self, bytes: &[u8])
Writes byte slice to hash.
Does hash * 33 + byte but is implemented as hash << 5 (*32) + hash + byte as this is faster on most processors vs normal multiplication.
fn write_u8(&mut self, i: u8)
1.3.0[src]
fn write_u8(&mut self, i: u8)
Writes a single u8
into this hasher.
fn write_u16(&mut self, i: u16)
1.3.0[src]
fn write_u16(&mut self, i: u16)
Writes a single u16
into this hasher.
fn write_u32(&mut self, i: u32)
1.3.0[src]
fn write_u32(&mut self, i: u32)
Writes a single u32
into this hasher.
fn write_u64(&mut self, i: u64)
1.3.0[src]
fn write_u64(&mut self, i: u64)
Writes a single u64
into this hasher.
fn write_u128(&mut self, i: u128)
1.26.0[src]
fn write_u128(&mut self, i: u128)
Writes a single u128
into this hasher.
fn write_usize(&mut self, i: usize)
1.3.0[src]
fn write_usize(&mut self, i: usize)
Writes a single usize
into this hasher.
fn write_i8(&mut self, i: i8)
1.3.0[src]
fn write_i8(&mut self, i: i8)
Writes a single i8
into this hasher.
fn write_i16(&mut self, i: i16)
1.3.0[src]
fn write_i16(&mut self, i: i16)
Writes a single i16
into this hasher.
fn write_i32(&mut self, i: i32)
1.3.0[src]
fn write_i32(&mut self, i: i32)
Writes a single i32
into this hasher.
fn write_i64(&mut self, i: i64)
1.3.0[src]
fn write_i64(&mut self, i: i64)
Writes a single i64
into this hasher.
fn write_i128(&mut self, i: i128)
1.26.0[src]
fn write_i128(&mut self, i: i128)
Writes a single i128
into this hasher.
fn write_isize(&mut self, i: isize)
1.3.0[src]
fn write_isize(&mut self, i: isize)
Writes a single isize
into this hasher.