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]

Creates a new hash using the original 5381 prime number salt value used by DJB.

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]

Examples

let input = "FY";
let mut hasher = X33a::new();
hasher.write(&input.as_bytes());
assert_eq!(hasher.finish(), 5862308u64);

impl Hasher for X33aU32
[src]

Returns the hash value for the values written so far. Read more

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.

Writes a single u8 into this hasher.

Writes a single u16 into this hasher.

Writes a single u32 into this hasher.

Writes a single u64 into this hasher.

Writes a single u128 into this hasher.

Writes a single usize into this hasher.

Writes a single i8 into this hasher.

Writes a single i16 into this hasher.

Writes a single i32 into this hasher.

Writes a single i64 into this hasher.

Writes a single i128 into this hasher.

Writes a single isize into this hasher.

Auto Trait Implementations

impl Send for X33aU32

impl Sync for X33aU32