/*makepw.c - accept a list of passwords, and save a corresponding list of encrypted passwords in the specified file. By Matthew Belmonte, October 1997. Copyright (c) 1197 by the Howard Hughes Medical Institute.*/ #include #include #include #include #include #include /*read a password from standard input*/ void get_password(s) char *s; { register int i, c; i = 0; printf("Password: "); while((i != 8) && ((c=getchar()) != EOF) && (c != '\n') && (c != '\r')) s[i++] = c; if(i == 8) while(((c=getchar()) != EOF) && (c != '\n') && (c != '\r')) ; s[i] = '\0'; } /*use the microseconds clock to generate a random seed*/ void set_random_seed() { struct timeval tv; gettimeofday(&tv, (struct timezone *)0); srandom((int)(tv.tv_usec >> 4)); } /*Generate a random character. set_random_seed() should have been called previously.*/ int get_random_char() { static char legal_chars[] = ":/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; return(legal_chars[(int)(random()&63L)]); } /*print an encrypted password (s) into the password file (fp)*/ void store_password(s, fp) char *s; FILE *fp; { char salt[3]; salt[2] = '\0'; set_random_seed(); salt[0] = get_random_char(); salt[1] = get_random_char(); fprintf(fp, "%s\n", crypt(s, salt)); } void main(argc, argv) int argc; char **argv; { FILE *cipherfile; char cleartext[81]; if(argc != 2) { fprintf(stderr, "usage: %s \n", *argv); exit(1); } umask(0177); cipherfile = fopen(argv[1], "w"); if(cipherfile == NULL) { perror(argv[1]); exit(errno); } get_password(cleartext); while(*cleartext) { store_password(cleartext, cipherfile); get_password(cleartext); } fclose(cipherfile); exit(0); }