21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

creates a modal dialog. A modal dialog can be used for capturing mouse messages<br />

instead of the modeless dialog that was required for gathering keyboard entropy in<br />

Recipe 11.20, because normal dialog processing doesn’t eat mouse messages the way<br />

it eats keyboard messages.<br />

Once the dialog is successfully created, the message handling procedure handles WM_<br />

MOUSEMOVE messages, which will be received whenever the mouse pointer moves over<br />

the dialog or its controls. The position of the mouse pointer is extracted from the<br />

message, converted to screen coordinates, combined with a timestamp, and fed into<br />

the hash object. If the current pointer position is the same as the previous pointer<br />

position, it is not counted as entropy but is added into the hash anyway. As mouse<br />

movements are collected, the progress bar is updated, and when the requested<br />

amount of entropy has been obtained, the OK button is enabled.<br />

When the OK button is clicked, the dialog is destroyed, terminating the message<br />

loop. The output from the hash function is copied into the output buffer from the<br />

caller, and internal data is cleaned up before returning to the caller.<br />

#include <br />

#include <br />

#include <br />

#define SPC_ENTROPY_PER_SAMPLE 0.5<br />

#define SPC_MOUSE_DLGID 102<br />

#define SPC_PROGRESS_BARID 1000<br />

#define SPC_MOUSE_COLLECTID 1003<br />

#define SPC_MOUSE_STATIC 1002<br />

typedef struct {<br />

double dEntropy;<br />

DWORD cbRequested;<br />

POINT ptLastPos;<br />

DWORD dwLastTime;<br />

HCRYPTHASH hHash;<br />

} SPC_DIALOGDATA;<br />

typedef struct {<br />

POINT ptMousePos;<br />

DWORD dwTickCount;<br />

} SPC_MOUSEPOS;<br />

static BOOL CALLBACK MouseEntropyProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,<br />

LPARAM lParam) {<br />

SPC_MOUSEPOS MousePos;<br />

SPC_DIALOGDATA *pDlgData;<br />

switch (uMsg) {<br />

case WM_INITDIALOG:<br />

pDlgData = (SPC_DIALOGDATA *)lParam;<br />

SetWindowLong(hwndDlg, DWL_USER, lParam);<br />

SendDlgItemMessage(hwndDlg, SPC_PROGRESS_BARID, PBM_SETRANGE32, 0,<br />

640 | 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!