21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

On Windows, the idea is the same, and the structure of the code is similar. Here is<br />

the same code as presented above, but implemented using the Win32 API:<br />

#include <br />

static DWORD WINAPI ThreadStub(LPVOID lpData) {<br />

return 0;<br />

}<br />

void SpcTimeThreads(DWORD dwIterCount) {<br />

DWORD dwThreadId;<br />

HANDLE hThread;<br />

while (dwIterCount--) {<br />

if ((hThread = CreateThread(0, 0, ThreadStub, 0, 0, &dwThreadId)) != 0) {<br />

WaitForSingleObject(hThread, INFINITE);<br />

CloseHandle(hThread);<br />

}<br />

}<br />

}<br />

See Recipe 4.14 for several different ways to get a timestamp. We strongly recommend<br />

that you use the most accurate method available on your platform.<br />

See Also<br />

Recipes 4.14, 11.19<br />

11.23 Gathering Entropy from System State<br />

<strong>Problem</strong><br />

You want to get some information that might actually change rapidly about the state<br />

of the kernel, in the hope that you might be able to get a bit of entropy from it.<br />

Solution<br />

The solution is highly operating system–specific. On systems with a /proc filesystem,<br />

you can read the contents of all the files in /proc. Otherwise, you can securely invoke<br />

commands that have some chance of providing entropy (especially if called infrequently).<br />

On Windows, the Performance Data Helper (PDH) API can be used to<br />

query much of the same type of information available on Unix systems with a /proc<br />

filesystem.<br />

Mix any data you collect, as well as a timestamp taken after the operation completes,<br />

into an entropy pool (see Recipe 11.19).<br />

644 | Chapter 11: Random Numbers<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!