{"id":387,"date":"2021-03-17T04:05:44","date_gmt":"2021-03-17T04:05:44","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=387"},"modified":"2021-03-17T16:16:29","modified_gmt":"2021-03-17T16:16:29","slug":"sessionid-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/03\/17\/sessionid-cs\/","title":{"rendered":"SessionId.cs"},"content":{"rendered":"\n<p>Session Id Class Hash Reduction Mapping<\/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;\npublic class SessionId\n{\n    private readonly SHA512Managed                _hash = new SHA512Managed();\n    private readonly AcDictionary&lt;byte[], byte[]> map   = new AcDictionary&lt;byte[], byte[]>(1024, new ArrayComparer());\n    public byte[] GetUniqueSessionId&lt;T>(T obj, int bitWidth)\n    {\n        if (bitWidth &lt; 32 || bitWidth > 256)\n            throw new ArgumentException($\"Bit Width {bitWidth} must be between 32 and 256.\");\n        var bytes = obj.GetBytes();\n        var hash  = _hash.ComputeHash(bytes, 0, bytes.Length);\n        map.Add(hash, bytes);\n        var ba = (byte[]) map.FindKeyIndex(hash).GetBytes().Clone();\n        return ba.SubArray(0, bitWidth >> 3);\n    }\n    public int GetUniqueSessionIntId&lt;T>(T obj)\n    {\n        var bytes = obj.GetBytes();\n        var hash  = _hash.ComputeHash(bytes, 0, bytes.Length);\n        map.Add(hash, bytes);\n        return map.FindKeyIndex(hash);\n    }\n    public BigInteger PermutationWithRepetition(BigInteger n, int r)\n    {\n        if (r == BigInteger.Zero)\n            return BigInteger.Zero;\n        if (n == BigInteger.Zero)\n            return BigInteger.Zero;\n        return BigInteger.Pow(n, r) - 1;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Session Id Class Hash Reduction Mapping<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[171,172],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/387"}],"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=387"}],"version-history":[{"count":2,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":394,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/387\/revisions\/394"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}