Created
January 12, 2021 14:06
-
-
Save mjs3339/168a21e4c5bbf0e10d66060cc02ff7af to your computer and use it in GitHub Desktop.
FNV1a Patterned 128Bit Hashing Algorithm using BigInteger
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.Numerics; | |
using System.Security.Cryptography; | |
public class FNV1a128b : HashAlgorithm | |
{ | |
private readonly BigInteger K = "309485009821345068724781371".BigIntegerBase10(); | |
private readonly BigInteger M = "340282366920938463463374607431768211456".BigIntegerBase10(); | |
private BigInteger _hash; | |
private readonly BigInteger Seed = "144066263297769815596495629667062367629".BigIntegerBase10(); | |
public FNV1a128b() | |
{ | |
_hash = Seed; | |
} | |
public FNV1a128b(BigInteger seed) | |
{ | |
Seed = seed; | |
_hash = Seed; | |
} | |
public override int HashSize => 128; | |
public override void Initialize() | |
{ | |
_hash = Seed; | |
} | |
protected override void HashCore(byte[] bytes, int ibStart, int cbSize) | |
{ | |
Hash128(bytes, ibStart, cbSize); | |
} | |
protected override byte[] HashFinal() | |
{ | |
return _hash.ToByteArray(); | |
} | |
private unsafe void Hash128(byte[] bytes, int ibStart, int cbSize) | |
{ | |
fixed (byte* pb = bytes) | |
{ | |
var np = pb + ibStart; | |
for (; cbSize > 0; --cbSize, np++) | |
_hash = ((_hash ^ *np) * K) % M; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment