{"id":421,"date":"2021-05-13T09:17:20","date_gmt":"2021-05-13T09:17:20","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=421"},"modified":"2021-05-13T09:17:20","modified_gmt":"2021-05-13T09:17:20","slug":"fnv1ax-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/05\/13\/fnv1ax-cs\/","title":{"rendered":"FNV1aX.cs"},"content":{"rendered":"\n<p>FNV1a 128&#8230;1024Bit Hashing BigInteger<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Numerics;\nusing System.Security.Cryptography;\nusing static System.Globalization.CultureInfo;\nusing static System.Globalization.NumberStyles;\nusing static System.Numerics.BigInteger;\n[Serializable]\npublic class FNV1aX : HashAlgorithm\n{\n    private static int        _bitWidth;\n    private        BigInteger _h1;\n    public         BigInteger _mod;\n    public         BigInteger _offset;\n    private        BigInteger _prime;\n    public FNV1aX(int bitWidth = 128)\n    {\n        _bitWidth = bitWidth;\n        SetBitWidthPom(bitWidth);\n        _h1 = _offset;\n    }\n    public override int HashSize => _bitWidth;\n    private void SetBitWidthPom(int bitWidth)\n    {\n        switch (bitWidth)\n        {\n            \/\/case 32:\n            \/\/    _prime  = Parse(\"1000193\", AllowHexSpecifier, InvariantCulture);\n            \/\/    _offset = Parse(\"811c9dc5\", AllowHexSpecifier, InvariantCulture);\n            \/\/    _mod    = Parse(\"10000000\", AllowHexSpecifier, InvariantCulture);\n            \/\/    break;\n            \/\/case 64:\n            \/\/    _prime  = Parse(\"100000001B3\", AllowHexSpecifier, InvariantCulture);\n            \/\/    _offset = Parse(\"CBF29CE484222325\", AllowHexSpecifier, InvariantCulture);\n            \/\/    _mod    = Parse(\"1000000000000000\", AllowHexSpecifier, InvariantCulture);\n            \/\/    break;\n            case 128:\n                _prime = Parse(\"0000000001000000000000000000013B\", AllowHexSpecifier, InvariantCulture);\n                _offset = Parse(\"6c62272e07bb014262b821756295c58d\", AllowHexSpecifier, InvariantCulture);\n                _mod = Parse(\"10000000000000000000000000000000\", AllowHexSpecifier, InvariantCulture);\n                break;\n            case 256:\n                _prime  = Parse(\"0000000000000000000001000000000000000000000000000000000000000163\", AllowHexSpecifier, InvariantCulture);\n                _offset = Parse(\"dd268dbcaac550362d98c384c4e576ccc8b1536847b6bbb31023b4c8caee0535\", AllowHexSpecifier, InvariantCulture);\n                _mod    = Parse(\"1000000000000000000000000000000000000000000000000000000000000000\", AllowHexSpecifier, InvariantCulture);\n                break;\n            case 512:\n                _prime = Parse(\"00000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000157\",\n                    AllowHexSpecifier, InvariantCulture);\n                _offset = Parse(\"b86db0b1171f4416dca1e50f309990acac87d059c90000000000000000000d21e948f68a34c192f62ea79bc942dbe7ce182036415f56e34bac982aac4afe9fd9\",\n                    AllowHexSpecifier, InvariantCulture);\n                _mod = Parse(\"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\", AllowHexSpecifier,\n                    InvariantCulture);\n                break;\n            case 1024:\n                _prime = Parse(\n                    \"000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018D\",\n                    AllowHexSpecifier, InvariantCulture);\n                _offset = Parse(\n                    \"0000000000000000005f7a76758ecc4d32e56d5a591028b74b29fc4223fdada16c3bf34eda3674da9a21d9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004c6d7eb6e73802734510a555f256cc005ae556bde8cc9c6a93b21aff4b16c71ee90b3\",\n                    AllowHexSpecifier, InvariantCulture);\n                _mod = Parse(\n                    \"1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\n                    AllowHexSpecifier, InvariantCulture);\n                break;\n        }\n    }\n    public override void Initialize()\n    {\n        _h1 = _offset;\n    }\n    protected override void HashCore(byte[] bytes, int ibStart, int cbSize)\n    {\n        Hash(bytes, ibStart, cbSize);\n    }\n    private unsafe void Hash(byte[] bytes, int ibStart, int cbSize)\n    {\n        if (bytes == null)\n            return;\n        if (ibStart >= bytes.Length || cbSize > bytes.Length)\n            return;\n        fixed (byte* pb = bytes)\n        {\n            var nb = pb + ibStart;\n            while (cbSize >= 8)\n            {\n                _h1    ^= *(ulong*) nb;\n                _h1    *= _prime;\n                _h1    %= _mod;\n                nb     += 8;\n                cbSize -= 8;\n            }\n            switch (cbSize &amp; 7)\n            {\n                case 7:\n                    _h1 ^= *(ulong*) (nb + 6);\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    goto case 6;\n                case 6:\n                    _h1 ^= *(ulong*) (nb + 5);\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    goto case 5;\n                case 5:\n                    _h1 ^= *(ulong*) (nb + 4);\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    goto case 4;\n                case 4:\n                    _h1 ^= *(ulong*) (nb + 3);\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    goto case 3;\n                case 3:\n                    _h1 ^= *(ulong*) (nb + 2);\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    goto case 2;\n                case 2:\n                    _h1 ^= *(ulong*) (nb + 1);\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    goto case 1;\n                case 1:\n                    _h1 ^= *nb;\n                    _h1 *= _prime;\n                    _h1 %= _mod;\n                    break;\n            }\n        }\n    }\n    protected override byte[] HashFinal()\n    {\n        return _h1.ToByteArray();\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>FNV1a 128&#8230;1024Bit Hashing BigInteger<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[183,67,182,149,14,166,76],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/421"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=421"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/421\/revisions"}],"predecessor-version":[{"id":422,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/421\/revisions\/422"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}