w32rng.c (1756B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include <windows.h> 7 #include <time.h> 8 #include <io.h> 9 #include <sys/types.h> 10 #include <sys/stat.h> 11 #include <stdio.h> 12 #include <primpl.h> 13 14 static BOOL CurrentClockTickTime(LPDWORD lpdwHigh, LPDWORD lpdwLow) { 15 LARGE_INTEGER liCount; 16 17 if (!QueryPerformanceCounter(&liCount)) { 18 return FALSE; 19 } 20 21 *lpdwHigh = liCount.u.HighPart; 22 *lpdwLow = liCount.u.LowPart; 23 return TRUE; 24 } 25 26 extern PRSize _PR_MD_GetRandomNoise(void* buf, PRSize size) { 27 DWORD dwHigh, dwLow, dwVal; 28 size_t n = 0; 29 size_t nBytes; 30 time_t sTime; 31 32 if (size <= 0) { 33 return 0; 34 } 35 36 CurrentClockTickTime(&dwHigh, &dwLow); 37 38 // get the maximally changing bits first 39 nBytes = sizeof(dwLow) > size ? size : sizeof(dwLow); 40 memcpy((char*)buf, &dwLow, nBytes); 41 n += nBytes; 42 size -= nBytes; 43 44 if (size <= 0) { 45 return n; 46 } 47 48 nBytes = sizeof(dwHigh) > size ? size : sizeof(dwHigh); 49 memcpy(((char*)buf) + n, &dwHigh, nBytes); 50 n += nBytes; 51 size -= nBytes; 52 53 if (size <= 0) { 54 return n; 55 } 56 57 // get the number of milliseconds that have elapsed since Windows started 58 dwVal = GetTickCount(); 59 60 nBytes = sizeof(dwVal) > size ? size : sizeof(dwVal); 61 memcpy(((char*)buf) + n, &dwVal, nBytes); 62 n += nBytes; 63 size -= nBytes; 64 65 if (size <= 0) { 66 return n; 67 } 68 69 // get the time in seconds since midnight Jan 1, 1970 70 time(&sTime); 71 nBytes = sizeof(sTime) > size ? size : sizeof(sTime); 72 memcpy(((char*)buf) + n, &sTime, nBytes); 73 n += nBytes; 74 75 return n; 76 }