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.

8.5 Generating Random Passwords and<br />

Passphrases<br />

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

You would like to avoid problems with easy-to-guess passwords by randomly generating<br />

passwords that are difficult to guess.<br />

Solution<br />

For passwords, choose random characters from an acceptable set of characters using<br />

spc_rand_range( ) (see Recipe 11.11). For passphrases, choose random words from a<br />

predefined list of acceptable words.<br />

Discussion<br />

In many situations, it may be desirable to present a user with a pregenerated password.<br />

For example, if the user is not present at the time of account creation, you will<br />

want to generate a reasonably secure password for the account and deliver the password<br />

to the user via some secure mechanism such as in person or over the phone.<br />

Randomly generated passwords are also useful when you want to enforce safe password<br />

requirements. If the user cannot supply an adequately secure password after a<br />

certain number of attempts, it may be best to present her with a randomly generated<br />

password to use, which will most likely pass all of the requirements tests.<br />

The primary disadvantage of randomly generated passwords is that they are usually<br />

difficult to memorize (and type), which often results in users writing them down. In<br />

many cases, however, this is a reasonable trade-off.<br />

The basic strategy for generating a random password is to define a character set that<br />

contains all of the characters that are valid for the type of password you are generating,<br />

then choose random members of that set until enough characters have been chosen<br />

to meet the length requirements.<br />

The string spc_password_characters defines the character set from which random<br />

password characters are chosen. The function spc_generate_password( ) requires a<br />

buffer and the size of the buffer as arguments. The buffer is filled with randomly chosen<br />

password characters and is properly NULL-terminated. As written, the function<br />

will always succeed, and it will return a pointer to the buffer filled with the randomly<br />

generated password.<br />

#include <br />

static char *spc_password_characters = "abcdefghijklmnopqrstuvwxyz0123456789"<br />

"ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*( )"<br />

"-=_+;[ ]{ }\\|,./?;";<br />

Generating Random Passwords and Passphrases | 387<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!