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.

salt[0] = choices[spc_rand_range(0, strlen(choices) - 1)];<br />

salt[1] = choices[spc_rand_range(0, strlen(choices) - 1)];<br />

salt[2] = 0;<br />

return crypt(password, salt);<br />

}<br />

Verifying a password encrypted with crypt( ) involves encrypting the plaintext password<br />

to be verified and comparing it with the already encrypted password, which<br />

would normally be obtained from the passwd structure returned by getpwnam( ) or<br />

getpwuid( ). (See Recipe 8.2.)<br />

Recall that crypt( ) stores the salt as the first two bytes of its result. For purposes of<br />

verification, you will not want to generate a random salt. Instead, you should use the<br />

already encrypted password as the salt.<br />

You can use the following function, spc_crypt_verify( ), to verify a password; however,<br />

we’re really only providing an example of how crypt( ) should be called to verify<br />

a password. It does little more than call crypt( ) and compare its result with the<br />

encrypted password.<br />

#include <br />

#include <br />

int spc_crypt_verify(const char *plain_password, const char *cipher_password) {<br />

return !strcmp(cipher_password, crypt(plain_password, cipher_password));<br />

}<br />

See Also<br />

Recipes 5.2, 8.2, 8.13<br />

8.10 Performing Password-Based<br />

Authentication with MD5-MCF<br />

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

You want to use MD5 as a method for encrypting passwords.<br />

Solution<br />

Many modern systems support the use of MD5 for encrypting passwords. An encoding<br />

known as Modular Crypt Format (MCF) is used to allow the use of the traditional<br />

crypt( ) function to handle the old DES encryption as well as MD5 and any<br />

number of other possible algorithms.<br />

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