- https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed
- https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
- http://www.isthe.com/chongo/tech/comp/fnv/index.html
- https://github.com/ssg/HashDepot
- https://gist.github.com/rasmuskl/3786618
Last active
May 9, 2020 05:43
-
-
Save jmrnilsson/902f2105f46b23838e7c20641ed70155 to your computer and use it in GitHub Desktop.
Fowler–Noll–Vo 1 A (FNV1) in 32 bit and 64 bit variants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
namespace Hashing | |
{ | |
public static class FowlerNollVo | |
{ | |
public static string ToFnv1aHashInt64(this string text) | |
{ | |
string Fnv1a(byte[] bytes_) | |
{ | |
const ulong offset = 14695981039346656037; | |
const ulong prime = 1099511628211; | |
ulong hash = offset; | |
for (var i = 0; i < bytes_.Length; i++) | |
{ | |
unchecked | |
{ | |
hash ^= bytes_[i]; | |
hash *= prime; | |
} | |
} | |
return Convert.ToBase64String(BitConverter.GetBytes(hash)); | |
} | |
byte[] bytes = Encoding.UTF8.GetBytes(text); | |
return Fnv1a(bytes); | |
} | |
public static string ToFnv1aHashInt32(this string text) | |
{ | |
string Fnv1a(byte[] bytes_) | |
{ | |
const uint offset = 0x811C9DC5; | |
const uint prime = 0x01000193; | |
uint hash = offset; | |
for (var i = 0; i < bytes_.Length; i++) | |
{ | |
unchecked | |
{ | |
hash ^= bytes_[i]; | |
hash *= prime; | |
} | |
} | |
// return BitConverter.ToInt64(bytes_, 0); | |
return Convert.ToBase64String(BitConverter.GetBytes(hash)); | |
} | |
byte[] bytes = Encoding.UTF8.GetBytes(text); | |
return Fnv1a(bytes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment