/*****************************************************************************/ /* */ /* File: appl.cpp */ /* */ /*****************************************************************************/ #include "appl.h" #include "globals.h" #include "prototypes.h" #include "global.h" #include "md5.h" #include <signal.h> // Signal/break key handling, #include <termios.h> // Terminal handling const int MAX_PASS_LEN = 8; /*****************************************************************************/ main () /* There are tons of things wrong with this program. Can you see what they are ? */ { char passwd[MAX_PASS_LEN]; char digest[16]; MD5_CTX context; strcpy (passwd,getpass("Enter password: ")); printf ("Your password was [%s]...\n",passwd); cfMD5Init(&context); cfMD5Update(&context,passwd,MAX_PASS_LEN); cfMD5Final(digest,&context); printf ("Md5 encrypted form is [%s]\n",cfMDPrint(digest)); } /*****************************************************************************/ char *getpass(const char *prompt) { static char buffer[MAX_PASS_LEN+1]; sigset_t sig,sigsave; struct termios term,termsave; FILE *fp; char *sp,ch; // Never mind the stupid C++ streams library. Use the C functions. if ((fp = fopen(ctermid(NULL),"r+")) == NULL) { perror("fopen"); return NULL; // What happens to the strcpy if this is NULL? } setbuf(fp,NULL); sigemptyset(&sig); sigaddset(&sig,SIGINT); sigaddset(&sig,SIGTSTP); sigprocmask(SIG_BLOCK,&sig,&sigsave); // On NT we have GetConsoleMode and SetConsoleMode ENABLE_ECHO_INPUT if (tcgetattr(fileno(fp),&termsave) == -1) { perror("tcgetattr"); sigprocmask(SIG_SETMASK,&sigsave,NULL); return NULL; } fputs("Switching off echo...\n",fp); term = termsave; term.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL); tcsetattr(fileno(fp),TCSAFLUSH,&term); fputs(prompt,fp); sp = buffer; while (ch = getc(fp)) { if (ch == '\n' || ch == EOF) { break; } if (sp < &(buffer[MAX_PASS_LEN])) { *sp++ = ch; // Feed the characters in manually } } *sp = '\0'; // Terminating character putc('\n',fp); // New line tcsetattr(fileno(fp),TCSAFLUSH,&termsave); fputs("Echo restored...\n",fp) ; sigprocmask(SIG_SETMASK,&sigsave,NULL); fclose(fp); return buffer; }