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.

#include <br />

#include <br />

static LPSTR Crypt64Encode(BYTE *pBuffer) {<br />

int i;<br />

DWORD dwTemp;<br />

LPSTR lpszOut, lpszPtr;<br />

static LPSTR lpszCrypt64Set = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"<br />

"abcdefghijklmnopqrstuvwyxz";<br />

if (!(lpszOut = lpszPtr = (char *)LocalAlloc(LMEM_FIXED, 23))) return 0;<br />

#define CRYPT64_ENCODE(x, y, z) \<br />

for (i = 0, dwTemp = (pBuffer[(x)] = 6) \<br />

*lpszPtr++ = lpszCrypt64Set[dwTemp & 0x3F]<br />

CRYPT64_ENCODE(0, 6, 12); CRYPT64_ENCODE(1, 7, 13);<br />

CRYPT64_ENCODE(2, 8, 14); CRYPT64_ENCODE(3, 9, 15);<br />

CRYPT64_ENCODE(4, 10, 5);<br />

for (i = 0, dwTemp = pBuffer[11]; i++ < 2; dwTemp >>= 6)<br />

*lpszPtr++ = lpszCrypt64Set[dwTemp & 0x3F];<br />

*lpszPtr = 0;<br />

#undef CRYPT64_ENCODE<br />

return lpszOut;<br />

}<br />

static BOOL ComputeHash(BYTE *pbHash, LPCSTR lpszKey, LPCSTR lpszSalt,<br />

DWORD dwSaltLength) {<br />

int i, length;<br />

DWORD cbHash, dwKeyLength;<br />

HCRYPTHASH hHash, hHash1;<br />

HCRYPTPROV hProvider;<br />

dwKeyLength = lstrlenA(lpszKey);<br />

if (!CryptAcquireContext(&hProvider, 0, MS_DEF_PROV, 0, CRYPT_VERIFYCONTEXT))<br />

return FALSE;<br />

if (!CryptCreateHash(hProvider, CALG_MD5, 0, 0, &hHash)) {<br />

CryptReleaseContext(hProvider, 0);<br />

return FALSE;<br />

}<br />

CryptHashData(hHash, (BYTE *)lpszKey, dwKeyLength, 0);<br />

CryptHashData(hHash, (BYTE *)lpszSalt, dwSaltLength, 0);<br />

if (!CryptCreateHash(hProvider, CALG_MD5, 0, 0, &hHash1)) {<br />

CryptDestroyHash(hHash);<br />

CryptReleaseContext(hProvider, 0);<br />

return FALSE;<br />

}<br />

CryptHashData(hHash1, lpszKey, dwKeyLength, 0);<br />

CryptHashData(hHash1, lpszSalt, dwSaltLength, 0);<br />

406 | Chapter 8: Authentication and Key Exchange<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!