Implementation of FNV hash in PHP

FNV is a simple algorithm and produces good results.

I needed this functionality as part of another project that required automated generation of relatively large number of unique usernames based on information provided by the user.

Some challenges:  Apparently PHP does not work well with numbers, especially large ones. I spent few hours trying to find out why my implementation was not producing correct results, … realized in the end that arithmetic with big numbers is something you should not do with PHP. To resolve these issues I had to use bitwise shift and add operations instead of multiply.

I put the code here: http://code.google.com/p/boyanov/source/browse/?repo=prototypes#hg/fnvhash.

… and a simple wiki page here: http://code.google.com/p/boyanov/wiki/FNVHash.

More information about the Fowler-Noll-Vo hash function could be found here:

Advertisement

One thought on “Implementation of FNV hash in PHP

Add yours

  1. Hi – you define FNV_prime_32 but I can’t see where you multiply the offset using this prime in the example code.

    Tom

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: