{"id":133,"date":"2020-07-30T08:59:31","date_gmt":"2020-07-30T08:59:31","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=133"},"modified":"2020-07-30T08:59:31","modified_gmt":"2020-07-30T08:59:31","slug":"mapping64bittohash32bit-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/30\/mapping64bittohash32bit-cs\/","title":{"rendered":"Mapping64BitToHash32Bit.cs"},"content":{"rendered":"\n<p>Maps a 64-Bit Hash Into a 32-Bit Space<\/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.Security.Cryptography;\n\/\/\/ &lt;summary>\n\/\/\/     Pros: ~no Collisions within int bit space ~(2,147,483,647)\n\/\/\/     Cons: Maintains a TinyDictionary(byte[],byte[]), non-deterministic across application or\n\/\/\/     method domains\n\/\/\/     Cannot Transform or reuse.\n\/\/\/ &lt;\/summary>\npublic class Mapping64BitToHash32Bit : HashAlgorithm\n{\n    private readonly FNV1a64                        hasher = new FNV1a64();\n    public readonly  TinyDictionary&lt;byte[], byte[]> map    = new TinyDictionary&lt;byte[], byte[]>(101, new ArrayComparer());\n    private          byte[]                         h64;\n    public override  int                            HashSize => 32;\n    public override void Initialize()\n    {\n    }\n    \/\/\/ &lt;inheritdoc \/>\n    \/\/\/ &lt;summary>\n    \/\/\/     Compute the 64 Bit hash value and add it and the original array to the map to create a unique position within the\n    \/\/\/     TinyDictionary.\n    \/\/\/ &lt;\/summary>\n    protected override void HashCore(byte[] bytes, int ibStart, int cbSize)\n    {\n        h64 = hasher.ComputeHash(bytes, ibStart, cbSize);\n        map.Add(h64, bytes);\n    }\n    \/\/\/ &lt;inheritdoc \/>\n    \/\/\/ &lt;summary>\n    \/\/\/     Return the unique position within the TinyDictionary as the hash value (index).\n    \/\/\/ &lt;\/summary>\n    protected override byte[] HashFinal()\n    {\n        HashValue = (byte[]) map.FindKeyIndex(h64).GetBytes().Clone();\n        return HashValue;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Maps a 64-Bit Hash Into a 32-Bit Space<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[65,61,62,60],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/133"}],"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=133"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}