{"id":128,"date":"2020-07-30T08:50:37","date_gmt":"2020-07-30T08:50:37","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=128"},"modified":"2020-07-30T08:50:37","modified_gmt":"2020-07-30T08:50:37","slug":"mapping160bitto64bithash-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/30\/mapping160bitto64bithash-cs\/","title":{"rendered":"Mapping160BitTo64BitHash.cs"},"content":{"rendered":"\n<p>Maps a 160-Bit Hash Into a 64-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 long bit space ~(9,223,372,036,854,775,807)\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 Mapping160BitTo64BitHash : HashAlgorithm\n{\n    public readonly  TinyDictionary&lt;byte[], byte[]> map = new TinyDictionary&lt;byte[], byte[]>(101, new ArrayComparer());\n    private          byte[]                         h160;\n    private readonly SHA1Managed                    hasher = new SHA1Managed();\n    public override  int                            HashSize => 64;\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        h160 = hasher.ComputeHash(bytes, ibStart, cbSize);\n        map.Add(h160, 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(h160).GetBytes().Clone();\n        return HashValue;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Maps a 160-Bit Hash Into a 64-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":[36,61,62,60],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/128"}],"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=128"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}