Back to index

glibc  2.9
genpass.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <time.h>
00003 #include <unistd.h>
00004 #include <crypt.h>
00005 
00006 int 
00007 main(void)
00008 {
00009   unsigned long seed[2];
00010   char salt[] = "$1$........";
00011   const char *const seedchars = 
00012     "./0123456789ABCDEFGHIJKLMNOPQRST"
00013     "UVWXYZabcdefghijklmnopqrstuvwxyz";
00014   char *password;
00015   int i;
00016   
00017   /* Generate a (not very) random seed.  
00018      You should do it better than this...  */
00019   seed[0] = time(NULL);
00020   seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
00021   
00022   /* Turn it into printable characters from `seedchars'.  */
00023   for (i = 0; i < 8; i++)
00024     salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
00025   
00026   /* Read in the user's password and encrypt it.  */
00027   password = crypt(getpass("Password:"), salt);
00028   
00029   /* Print the results.  */
00030   puts(password);
00031   return 0;
00032 }