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.

}<br />

lpszBase64Salt[dwSaltLength] = 0;<br />

if (!ComputeHash(pbHash, lpszKey, lpszBase64Salt, dwSaltLength)) goto done;<br />

if (!(lpszBase64Out = Crypt64Encode(pbHash))) goto done;<br />

dwResultLength = lstrlenA(lpszBase64Out) + lstrlenA(lpszBase64Salt) + 5;<br />

if (!(lpszResult = (LPSTR)LocalAlloc(LMEM_FIXED, dwResultLength + 1)))<br />

goto done;<br />

wsprintfA(lpszResult, "$1$%s$%s", lpszBase64Salt, lpszBase64Out);<br />

done:<br />

/* cleanup */<br />

if (lpszBase64Salt) LocalFree(lpszBase64Salt);<br />

if (lpszBase64Out) LocalFree(lpszBase64Out);<br />

return lpszResult;<br />

}<br />

Verifying a password encrypted using MD5-MCF works the same way as verifying a<br />

password encrypted with crypt( ): encrypt the plaintext password with the already<br />

encrypted password as the salt, and compare the result with the already encrypted<br />

password. If they match, the password is correct.<br />

For the sake of both consistency and convenience, you can use the function spc_md5_<br />

verify( ) to verify a password encrypted using MD5-MCF.<br />

int spc_md5_verify(const char *plain_password, const char *crypt_password) {<br />

int match = 0;<br />

char *md5_result;<br />

if ((md5_result = spc_md5_encrypt(plain_password, crypt_password)) != 0) {<br />

match = !strcmp(md5_result, crypt_password);<br />

free(md5_result);<br />

}<br />

return match;<br />

}<br />

See Also<br />

Recipes 8.9, 8.11<br />

8.11 Performing Password-Based<br />

Authentication with PBKDF2<br />

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

You want to use a stronger encryption method than crypt( ) and MD5-MCF (see<br />

Recipes 8.9 and 8.10).<br />

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