Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# FNV1a 1024Bit Implementation
public class FNV1a1024 : HashAlgorithm
{
private readonly BigInteger _K = "5016456510113118655434598811035278955030765345404790744303017523831112055108147451509157692220295382716162651878526895249385292291816524375083746691371804094271873160484737966720260389217684476157468082573".NewBigInteger10();
private BigInteger _hash;
private readonly BigInteger Seed = "14197795064947621068722070641403218320880622795441933960878474914617582723252296732303717722150864096521202355549365628174669108571814760471015076148029755969804077320157692458563003215304957150157403644460363550505412711285966361610267868082893823963790439336411086884584107735010676915".NewBigInteger10();
public FNV1a1024()
{
_hash = Seed;
}
public FNV1a1024(string seed)
{
Seed = seed.NewBigInteger10();
_hash = Seed;
}
public override int HashSize => 1024;
public override void Initialize()
{
_hash = Seed;
}
protected override void HashCore(byte[] bytes, int ibStart, int cbSize)
{
Hash(bytes, ibStart, cbSize);
}
private void Hash(byte[] bytes, int ibStart, int cbSize)
{
if (bytes == null)
return;
if (ibStart >= bytes.Length || cbSize > bytes.Length)
return;
var p = 0;
while (cbSize > 0)
{
_hash ^= bytes[p + ibStart];
_hash *= _K;
cbSize--;
p++;
}
_hash = _hash ^ (_hash >> 512);
}
/// <summary>
/// BigInteger does not overflow, so we limit the output to 1024Bits.
/// </summary>
protected override byte[] HashFinal()
{
return _hash.ToByteArray().SubByte(0, 128);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment