FNV1a 128…1024Bit Hashing BigInteger
using System; using System.Numerics; using System.Security.Cryptography; using static System.Globalization.CultureInfo; using static System.Globalization.NumberStyles; using static System.Numerics.BigInteger; [Serializable] public class FNV1aX : HashAlgorithm { private static int _bitWidth; private BigInteger _h1; public BigInteger _mod; public BigInteger _offset; private BigInteger _prime; public FNV1aX(int bitWidth = 128) { _bitWidth = bitWidth; SetBitWidthPom(bitWidth); _h1 = _offset; } public override int HashSize => _bitWidth; private void SetBitWidthPom(int bitWidth) { switch (bitWidth) { //case 32: // _prime = Parse("1000193", AllowHexSpecifier, InvariantCulture); // _offset = Parse("811c9dc5", AllowHexSpecifier, InvariantCulture); // _mod = Parse("10000000", AllowHexSpecifier, InvariantCulture); // break; //case 64: // _prime = Parse("100000001B3", AllowHexSpecifier, InvariantCulture); // _offset = Parse("CBF29CE484222325", AllowHexSpecifier, InvariantCulture); // _mod = Parse("1000000000000000", AllowHexSpecifier, InvariantCulture); // break; case 128: _prime = Parse("0000000001000000000000000000013B", AllowHexSpecifier, InvariantCulture); _offset = Parse("6c62272e07bb014262b821756295c58d", AllowHexSpecifier, InvariantCulture); _mod = Parse("10000000000000000000000000000000", AllowHexSpecifier, InvariantCulture); break; case 256: _prime = Parse("0000000000000000000001000000000000000000000000000000000000000163", AllowHexSpecifier, InvariantCulture); _offset = Parse("dd268dbcaac550362d98c384c4e576ccc8b1536847b6bbb31023b4c8caee0535", AllowHexSpecifier, InvariantCulture); _mod = Parse("1000000000000000000000000000000000000000000000000000000000000000", AllowHexSpecifier, InvariantCulture); break; case 512: _prime = Parse("00000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000157", AllowHexSpecifier, InvariantCulture); _offset = Parse("b86db0b1171f4416dca1e50f309990acac87d059c90000000000000000000d21e948f68a34c192f62ea79bc942dbe7ce182036415f56e34bac982aac4afe9fd9", AllowHexSpecifier, InvariantCulture); _mod = Parse("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", AllowHexSpecifier, InvariantCulture); break; case 1024: _prime = Parse( "000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018D", AllowHexSpecifier, InvariantCulture); _offset = Parse( "0000000000000000005f7a76758ecc4d32e56d5a591028b74b29fc4223fdada16c3bf34eda3674da9a21d9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004c6d7eb6e73802734510a555f256cc005ae556bde8cc9c6a93b21aff4b16c71ee90b3", AllowHexSpecifier, InvariantCulture); _mod = Parse( "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", AllowHexSpecifier, InvariantCulture); break; } } public override void Initialize() { _h1 = _offset; } protected override void HashCore(byte[] bytes, int ibStart, int cbSize) { Hash(bytes, ibStart, cbSize); } private unsafe void Hash(byte[] bytes, int ibStart, int cbSize) { if (bytes == null) return; if (ibStart >= bytes.Length || cbSize > bytes.Length) return; fixed (byte* pb = bytes) { var nb = pb + ibStart; while (cbSize >= 8) { _h1 ^= *(ulong*) nb; _h1 *= _prime; _h1 %= _mod; nb += 8; cbSize -= 8; } switch (cbSize & 7) { case 7: _h1 ^= *(ulong*) (nb + 6); _h1 *= _prime; _h1 %= _mod; goto case 6; case 6: _h1 ^= *(ulong*) (nb + 5); _h1 *= _prime; _h1 %= _mod; goto case 5; case 5: _h1 ^= *(ulong*) (nb + 4); _h1 *= _prime; _h1 %= _mod; goto case 4; case 4: _h1 ^= *(ulong*) (nb + 3); _h1 *= _prime; _h1 %= _mod; goto case 3; case 3: _h1 ^= *(ulong*) (nb + 2); _h1 *= _prime; _h1 %= _mod; goto case 2; case 2: _h1 ^= *(ulong*) (nb + 1); _h1 *= _prime; _h1 %= _mod; goto case 1; case 1: _h1 ^= *nb; _h1 *= _prime; _h1 %= _mod; break; } } } protected override byte[] HashFinal() { return _h1.ToByteArray(); } }