Function Repository Resource:

FowlerNollVoHash

Source Notebook

Hash an expression using Fowler-Noll-Vo hashing

Contributed by: Jan Mangaldan

ResourceFunction["FowlerNollVoHash"][expr]

gives the Fowler-Noll-Vo (FNV) hash code for the expression expr.

ResourceFunction["FowlerNollVoHash"][expr,n]

gives the n-bit hash.

Details and Options

ResourceFunction["FowlerNollVoHash"][expr] is equivalent to ResourceFunction["FowlerNollVoHash"][expr,32], corresponding to a 32-bit hash.
n must be a power of 2 between 32 and 1024.
ResourceFunction["FowlerNollVoHash"] computes the FNV-1a hash by default (here the "a" refers to "alternate").
ResourceFunction["FowlerNollVoHash"] takes the following options:
"UseAlternate"Truewhether to use the alternate algorithm
"UseShift"Truewhether to do an initial shift
With "UseAlternate"False and "UseShift"True, FNV-1 hashing is performed.
With "UseAlternate"False and "UseShift"False, FNV-0 hashing is performed.

Examples

Basic Examples (2) 

The 32-bit FNV-1a hash of a string:

In[1]:=
ResourceFunction["FowlerNollVoHash"]["abcdef"]
Out[1]=

Use 64 bits:

In[2]:=
ResourceFunction["FowlerNollVoHash"]["abcdef", 64]
Out[2]=

Scope (4) 

64-bit FNV-1a hash of a long string:

In[3]:=
ResourceFunction[
 "FowlerNollVoHash"]["The quick brown fox jumps over the lazy dog", 64]
Out[3]=

Display in hexadecimal format:

In[4]:=
IntegerString[%, 16]
Out[4]=

Use ByteArray to hash literal bytes:

In[5]:=
ResourceFunction["FowlerNollVoHash"][ByteArray[{97, 98, 99}], 128]
Out[5]=

An equivalent specification:

In[6]:=
ResourceFunction["FowlerNollVoHash"][
 ByteArrayToString@ByteArray[{97, 98, 99}], 128]
Out[6]=

Hash a file:

In[7]:=
ResourceFunction["FowlerNollVoHash"][
 File["ExampleData/ocelot.jpg"], 64]
Out[7]=

Hash a general expression:

In[8]:=
ResourceFunction["FowlerNollVoHash"][Graphics[Disk[]]]
Out[8]=

Options (2) 

UseAlternate (1) 

Do not use the "alternate" method for FNV hashing:

In[9]:=
ResourceFunction[
 "FowlerNollVoHash"]["The quick brown fox jumps over the lazy dog", "UseAlternate" -> False]
Out[9]=

UseShift (1) 

Set "UseShift"False to avoid using a shift when hashing. Not using a shift may lead to more hash collisions:

In[10]:=
ResourceFunction[
 "FowlerNollVoHash"]["The quick brown fox jumps over the lazy dog", "UseShift" -> False]
Out[10]=

Applications (3) 

Provide a "checksum" to validate data integrity:

In[11]:=
data = RandomInteger[10, {1000}];
check = ResourceFunction["FowlerNollVoHash"][data]
Out[12]=

Change some of the data:

In[13]:=
data[[500]] = data[[500]] + 1;

The checksum has changed:

In[14]:=
ResourceFunction["FowlerNollVoHash"][data]
Out[14]=
In[15]:=
% == check
Out[15]=

Neat Examples (2) 

A string that gives a 64-bit FNV-1a hash of 0:

In[16]:=
ResourceFunction["FowlerNollVoHash"]["Õk¹SB‡\.086", 64]
Out[16]=

A string that gives a 32-bit FNV-1 hash of 0:

In[17]:=
ResourceFunction["FowlerNollVoHash"]["ýEA\.08 ", "UseAlternate" -> False]
Out[17]=

Version History

  • 1.0.0 – 06 April 2021

Related Resources

License Information