{"id":156,"date":"2020-08-06T07:12:52","date_gmt":"2020-08-06T07:12:52","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=156"},"modified":"2020-08-06T07:12:52","modified_gmt":"2020-08-06T07:12:52","slug":"crc64cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/08\/06\/crc64cs\/","title":{"rendered":"CRC64cs"},"content":{"rendered":"\n<p>Standard CRC 64 <\/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.Globalization;\nusing System.Security.Cryptography;\nusing System.Text;\npublic class CRC64 : HashAlgorithm\n{\n    \/\/\/ &lt;summary>\n    \/\/\/     https:\/\/en.wikipedia.org\/wiki\/Polynomial_representations_of_cyclic_redundancy_checks\n    \/\/\/ &lt;\/summary>\n    public enum PolyType : ulong\n    {\n        ECMA182 = 0xD800000000000000,\n        ISOPoly = 0xC96C5795D7870F42\n    }\n    private const  ulong   Seed = 0x0;\n    private static ulong[] _table;\n    private        ulong   _hash;\n    public CRC64(PolyType pt = PolyType.ECMA182)\n    {\n        HashSizeValue = 64;\n        InitializeTable((ulong) pt);\n        Initialize();\n    }\n    public override void Initialize()\n    {\n        _hash = Seed;\n    }\n    protected override void HashCore(byte[] data, int start, int size)\n    {\n        State = 1;\n        for (var i = start; i &lt; start + size; i++)\n            unchecked\n            {\n                _hash = (_hash >> 8) ^ _table[(data[i] ^ _hash) &amp; 0xff];\n            }\n    }\n    protected override byte[] HashFinal()\n    {\n        HashValue = UInt64ToBigEndianBytes(_hash);\n        State     = 0;\n        _hash     = Seed;\n        return HashValue;\n    }\n    private static byte[] UInt64ToBigEndianBytes(ulong value)\n    {\n        var result = BitConverter.GetBytes(value);\n        if (BitConverter.IsLittleEndian)\n            Array.Reverse(result);\n        return result;\n    }\n    public static string ToHex(byte[] data)\n    {\n        var builder = new StringBuilder();\n        foreach (var item in data)\n            builder.Append(item.ToString(\"X2\", CultureInfo.InvariantCulture));\n        return builder.ToString();\n    }\n    private static void InitializeTable(ulong polynomial)\n    {\n        _table = new ulong[256];\n        for (var i = 0; i &lt; 256; ++i)\n        {\n            var entry = (ulong) i;\n            for (var j = 0; j &lt; 8; ++j)\n                if ((entry &amp; 1) == 1)\n                    entry = (entry >> 1) ^ polynomial;\n                else\n                    entry = entry >> 1;\n            _table[i] = entry;\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Standard CRC 64<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[72,39],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/156"}],"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=156"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/156\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}