{"id":101,"date":"2020-06-28T14:08:01","date_gmt":"2020-06-28T14:08:01","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=101"},"modified":"2020-06-28T14:08:01","modified_gmt":"2020-06-28T14:08:01","slug":"entropysource-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/06\/28\/entropysource-cs\/","title":{"rendered":"EntropySource.cs"},"content":{"rendered":"\n<p>Using PC Data Creates a Strong Entropy Source<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Generic;\nusing System.Diagnostics;\nusing System.Linq;\nusing System.Runtime.InteropServices;\nusing System.Threading;\npublic class EntropySource\n{\n    private const int Sha3BitSize           = 256;\n    private const int Sha3BitBufferMoveSize = Sha3BitSize >> 3;\n    private const int CpuDataLength         = 256;\n    private const int JitterDataSize        = 256;\n    [DllImport(\"kernel32.dll\")]\n    private static extern int GetTickCount();\n    [DllImport(\"kernel32.dll\")]\n    internal static extern bool QueryPerformanceCounter(out long lpPerformanceCount);\n    [DllImport(\"kernel32.dll\")]\n    internal static extern bool QueryPerformanceFrequency(out long lpFrequency);\n    public static byte[] GetBuffer(int bufferSize)\n    {\n        var rhb = new byte[0];\n        var rho = new byte[bufferSize];\n        var EntropyThread = new Thread(() =>\n        {\n            var el        = new List&lt;string>();\n            var hashBytes = new HashSet&lt;byte[]>(new ArrayComparer());\n            var sw        = new Stopwatch();\n            sw.Start();\n            AddLocalUserData(hashBytes);\n            AddProcessInformation(hashBytes, el);\n            AddJitterInformation(hashBytes);\n            GetWmiInfor(\"Win32_PerfRawData_PerfOS_Processor\",              hashBytes);\n            GetWmiInfor(\"Win32_PerfRawData_PerfOS_System\",                 hashBytes);\n            GetWmiInfor(\"Win32_PerfRawData_Counters_ProcessorInformation\", hashBytes);\n            GetWmiInfor(\"Win32_PerfRawData_Counters_Synchronization\",      hashBytes);\n            GetWmiInfor(\"Win32_PerfRawData_PerfDisk_LogicalDisk\",          hashBytes);\n            GetWmiInfor(\"Win32_PerfRawData_PerfDisk_PhysicalDisk\",         hashBytes);\n            GetWmiInfor(\"Win32_PerfFormattedData_PerfOS_Cache\",            hashBytes);\n            GetWmiInfor(\"Win32_PerfFormattedData_PerfOS_Memory\",           hashBytes);\n            GetWmiInfor(\"Win32_PerfFormattedData_PerfOS_Objects\",          hashBytes);\n            CpuInfo(hashBytes);\n            sw.Stop();\n            hashBytes.Add(sw.Elapsed.Ticks.GetBytes());\n            rhb = HashBytesToArray(hashBytes);\n            AddHistogramInforamation(rhb, hashBytes);\n            rhb = HashBytesToArray(hashBytes);\n            hashBytes.Clear();\n            var div = bufferSize \/ Sha3BitBufferMoveSize;\n            if (div * Sha3BitBufferMoveSize &lt; bufferSize)\n                div++;\n            var segSize = rhb.Length \/ div;\n            var aseg    = new byte[segSize];\n            for (var i = 0; i &lt; div; ++i)\n            {\n                Buffer.BlockCopy(rhb,                                            i * segSize, aseg, 0,                         segSize);\n                Buffer.BlockCopy(new SHA3Managed(Sha3BitSize).ComputeHash(aseg), 0,           rho,  i * Sha3BitBufferMoveSize, Sha3BitBufferMoveSize);\n            }\n        }) {Priority = ThreadPriority.Highest};\n        EntropyThread.Start();\n        EntropyThread.Join();\n        return rho;\n    }\n    private static void GetWmiInfor(string key, HashSet&lt;byte[]> hashBytes)\n    {\n        var wmi = new WMIServices();\n        wmi.GetProperties(key);\n        foreach (var v in wmi.EntriesList)\n        foreach (var p in v.Value)\n            hashBytes.Add(p.GetBytesObject());\n    }\n    private static void CpuInfo(HashSet&lt;byte[]> hashBytes)\n    {\n        var lhashBytes = new HashSet&lt;byte[]>();\n        var dump       = CpuTotalPc.CPULoad;\n        do\n        {\n            var l = (byte) CpuTotalPc.CPULoad;\n            if (l != 0)\n                lhashBytes.Add(new[] {l});\n            Thread.Sleep(1);\n        } while (lhashBytes.Count &lt; CpuDataLength);\n        foreach (var b in lhashBytes)\n            hashBytes.Add(b);\n    }\n    private static void AddHistogramInforamation(byte[] rhb, HashSet&lt;byte[]> hashBytes)\n    {\n        var FFTArray = Histogram(rhb);\n        for (var i = 0; i &lt; FFTArray.Length; ++i)\n            hashBytes.Add(FFTArray[i].GetBytes());\n    }\n    private static void AddJitterInformation(HashSet&lt;byte[]> hashBytes)\n    {\n        hashBytes.Add(new JitterEx(JitterDataSize).GetBuffer());\n    }\n    private static void AddProcessInformation(HashSet&lt;byte[]> hashBytes, List&lt;string> el)\n    {\n        foreach (var p in Process.GetProcesses())\n        {\n            try\n            {\n                hashBytes.Add(p.Id.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.Id.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.HandleCount.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.HandleCount.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.NonpagedSystemMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.NonpagedSystemMemorySize64.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.PagedMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.PagedMemorySize64.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.PagedSystemMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.PagedSystemMemorySize64.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.PeakPagedMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.PeakPagedMemorySize64.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.PeakVirtualMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.PeakVirtualMemorySize64.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.PeakWorkingSet64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.PeakWorkingSet64.GetBytes()\");\n            }\n            try\n            {\n                hashBytes.Add(p.PrivateMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.PrivateMemorySize64.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.ProcessName.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.ProcessName.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.Threads.Count.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.Threads.Count.GetBytes())\");\n            }\n            try\n            {\n                hashBytes.Add(p.SessionId.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.SessionId.GetBytes())\");\n            }\n            for (var i = 0; i &lt; p.Threads.Count; i++)\n            {\n                try\n                {\n                    hashBytes.Add(p.Threads[i].CurrentPriority.GetBytes());\n                }\n                catch (Exception ex)\n                {\n                    el.Add($\"{ex.Message} hashBytes.Add(p.Threads[i].CurrentPriority.GetBytes())\");\n                }\n                try\n                {\n                    hashBytes.Add(p.Threads[i].Id.GetBytes());\n                }\n                catch (Exception ex)\n                {\n                    el.Add($\"{ex.Message} hashBytes.Add(p.Threads[i].Id.GetBytes())\");\n                }\n                try\n                {\n                    hashBytes.Add(p.Threads[i].StartAddress.ToString().GetBytes());\n                }\n                catch (Exception ex)\n                {\n                    el.Add($\"{ex.Message} hashBytes.Add(p.Threads[i].StartAddress.ToString().GetBytes())\");\n                }\n            }\n            try\n            {\n                hashBytes.Add(p.VirtualMemorySize64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.VirtualMemorySize64.GetBytes()))\");\n            }\n            try\n            {\n                hashBytes.Add(p.WorkingSet64.GetBytes());\n            }\n            catch (Exception ex)\n            {\n                el.Add($\"{ex.Message} hashBytes.Add(p.WorkingSet64.GetBytes()))\");\n            }\n        }\n    }\n    private static void AddLocalUserData(HashSet&lt;byte[]> hashBytes)\n    {\n        hashBytes.Add(Process.GetCurrentProcess().Id.GetBytes());\n        hashBytes.Add(Thread.CurrentThread.ManagedThreadId.GetBytes());\n        hashBytes.Add(GetTickCount().GetBytes());\n        hashBytes.Add(Process.GetProcesses().Length.GetBytes());\n        QueryPerformanceFrequency(out var freq);\n        hashBytes.Add(freq.GetBytes());\n        for (var i = 0; i &lt; 128; i++)\n        {\n            QueryPerformanceCounter(out var query);\n            hashBytes.Add(query.GetBytes());\n        }\n        hashBytes.Add(DateTime.Now.Ticks.GetBytes());\n        hashBytes.Add(Environment.UserName.GetBytes());\n        hashBytes.Add(Environment.MachineName.GetBytes());\n        hashBytes.Add(Environment.OSVersion.ToString().GetBytes());\n        hashBytes.Add(Environment.ProcessorCount.GetBytes());\n        hashBytes.Add(Environment.UserDomainName.GetBytes());\n        hashBytes.Add(Environment.StackTrace.GetBytes());\n        hashBytes.Add(Environment.GetLogicalDrives().GetBytes());\n        hashBytes.Add(MemoryInfo.GetValues().GetBytesObjectSerial());\n        hashBytes.Add(MemoryInfo.GetSystemPerformanceInformation().GetBytesObjectSerial());\n    }\n    private static byte[] HashBytesToArray(HashSet&lt;byte[]> hashBytes)\n    {\n        byte[] rhb;\n        var    ptr   = 0;\n        var    count = 0;\n        var    arr   = hashBytes.ToArray();\n        foreach (var hba in arr)\n            count += hba.Length;\n        hashBytes.Add(count.GetBytes());\n        arr   =  hashBytes.ToArray();\n        count += arr.Last().Length;\n        rhb   =  new byte[count];\n        foreach (var hba in arr)\n        {\n            Buffer.BlockCopy(hba, 0, rhb, ptr, hba.Length);\n            ptr += hba.Length;\n        }\n        return rhb;\n    }\n    private static int[] Histogram(IEnumerable&lt;byte> ba)\n    {\n        var map = new int[256];\n        foreach (var i in ba)\n            map[i]++;\n        return map;\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using PC Data Creates a Strong Entropy Source<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[5,44,45,43],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/101"}],"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=101"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":102,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/101\/revisions\/102"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}