{"id":130,"date":"2020-07-30T08:56:07","date_gmt":"2020-07-30T08:56:07","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=130"},"modified":"2021-01-03T14:45:19","modified_gmt":"2021-01-03T14:45:19","slug":"130","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/30\/130\/","title":{"rendered":"FNV1a64.cs"},"content":{"rendered":"\n<p>Fowler\u2013Noll\u2013Vo hash function 64-Bit<\/p>\n\n\n\n<p>Updated: Jan-3,2021<\/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.Security.Cryptography;\n[Serializable]\npublic class FNV1a64 : HashAlgorithm\n{\n    private const ulong K = 0x100000001B3;\n    private const ulong M = 0x1000000000000000;\n    public        ulong _hash;\n    public        ulong Hash;\n    public        ulong Seed = 0xCBF29CE484222325;\n    public FNV1a64()\n    {\n        _hash = Seed;\n    }\n    public FNV1a64(ulong seed)\n    {\n        Seed  = seed;\n        _hash = Seed;\n    }\n    public override int HashSize => 64;\n    public override void Initialize()\n    {\n        _hash = Seed;\n    }\n    protected override void HashCore(byte[] bytes, int ibStart, int cbSize)\n    {\n        Hash64(bytes, ibStart, cbSize);\n    }\n    private unsafe void Hash64(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                _hash  ^= *(ulong*) nb;\n                nb     += 8;\n                cbSize -= 8;\n                _hash  *= K;\n                _hash  %= M;\n            }\n            switch (cbSize &amp; 7)\n            {\n                case 7:\n                    _hash ^= *(ulong*) (nb + 6);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 6;\n                case 6:\n                    _hash ^= *(ulong*) (nb + 5);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 5;\n                case 5:\n                    _hash ^= *(ulong*) (nb + 4);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 4;\n                case 4:\n                    _hash ^= *(ulong*) (nb + 3);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 3;\n                case 3:\n                    _hash ^= *(ulong*) (nb + 2);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 2;\n                case 2:\n                    _hash ^= *(ulong*) (nb + 1);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 1;\n                case 1:\n                    _hash ^= *nb;\n                    _hash *= K;\n                    _hash %= M;\n                    break;\n            }\n        }\n    }\n    public unsafe ulong Hash64(byte[] bytes)\n    {\n        var cbSize = bytes.Length;\n        _hash = Seed;\n        fixed (byte* pb = bytes)\n        {\n            var nb = pb;\n            while (cbSize >= 8)\n            {\n                _hash  ^= *(ulong*) nb;\n                nb     += 8;\n                cbSize -= 8;\n                _hash  *= K;\n                _hash  %= M;\n            }\n            switch (cbSize &amp; 7)\n            {\n                case 7:\n                    _hash ^= *(ulong*) (nb + 6);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 6;\n                case 6:\n                    _hash ^= *(ulong*) (nb + 5);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 5;\n                case 5:\n                    _hash ^= *(ulong*) (nb + 4);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 4;\n                case 4:\n                    _hash ^= *(ulong*) (nb + 3);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 3;\n                case 3:\n                    _hash ^= *(ulong*) (nb + 2);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 2;\n                case 2:\n                    _hash ^= *(ulong*) (nb + 1);\n                    _hash *= K;\n                    _hash %= M;\n                    goto case 1;\n                case 1:\n                    _hash ^= *nb;\n                    _hash *= K;\n                    _hash %= M;\n                    break;\n            }\n        }\n        return _hash;\n    }\n    protected override byte[] HashFinal()\n    {\n        Hash = _hash;\n        return _hash.GetBytes();\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Fowler\u2013Noll\u2013Vo hash function 64-Bit Updated: Jan-3,2021<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[64,63,61],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/130"}],"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=130"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}