Listing 1: Encrypting a password
/*****************************************************************************
* Listing 1 *
*****************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <limits.h>
extern char *getpass ();
extern char *crypt ();
/*
* The salt is a two character string chosen from the set [a-zA-Z0-9./]
*/
static char alphabet [] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
int
make_passwd (passwd, encrypted)
char *passwd;
char *encrypted;
{
char *cp;
char salt [3];
int random;
unsigned int seed;
time_t t;
/*
* To generate a random "salt", we first pick a
* "seed" to set a random starting point.
*/
time (&t);
seed = (unsigned int) (t % INT_MAX);
srand (seed);
/*
* Pick two random characters from the salt alphabet
*/
random = ((rand ()) >> 4) % 64;
salt [0] = alphabet [random];
random = ((rand ()) >> 4) % 64;
salt [1] = alphabet [random];
salt [2] = '\0';
/*
* Encrypt the password
*/
cp = crypt (passwd, salt);
/*
* Store the encrypted string in
* the given argument.
*/
strcpy (encrypted, cp);
return 0;
}
|