#include #include #include "md5.h" #include "random.h" #include "externs.h" #include "strings.h" #include "pw-a.h" /* don't casually change either of these; too many things depend on them */ static const char alphabet[] = "!\"#$%&'()*+,-./0123456789:;<=>?@" "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" "abcdefghijklmnopqrstuvwxyz{|}~"; #define BASE 94 static unsigned int base_div(unsigned char *v) { unsigned int r; int i; r = 0; for (i=0;i<16;i++) { r = (r << 8) + v[i]; v[i] = r / BASE; r %= BASE; } return(r); } static void base_expand(const unsigned char *res, char *txt) { int i; unsigned char tmp[16]; bcopy(res,&tmp[0],16); for (i=0;i<20;i++) txt[i] = alphabet[base_div(&tmp[0])]; } int pw_a_check(const char *pass, const char *plain) { void *s; unsigned char md5res[16]; char text[20]; if (strlen(pass) != 40) return(1); s = md5_init(); md5_process_bytes(s,plain,strlen(plain)); md5_process_bytes(s,pass,20); md5_result(s,&md5res[0]); base_expand(&md5res[0],&text[0]); return(bcmp(&text[0],pass+20,20)); } char *pw_a_make(const char *plain) { void *s; unsigned char binsalt[9]; unsigned char md5res[16]; char *text; struct timeval tv; text = malloc(41); gettimeofday(&tv,0); binsalt[0] = tv.tv_sec & 0xff; binsalt[1] = (tv.tv_sec >> 8) & 0xff; binsalt[2] = (tv.tv_sec >> 16) & 0xff; binsalt[3] = (tv.tv_sec >> 24) & 0xff; binsalt[4] = rndint(); binsalt[5] = tv.tv_usec & 0xff; binsalt[6] = (tv.tv_usec >> 8) & 0xff; binsalt[7] = (tv.tv_usec >> 16) & 0xff; binsalt[8] = (tv.tv_usec >> 24) & 0xff; s = md5_init(); md5_process_bytes(s,&binsalt[0],9); md5_result(s,&md5res[0]); base_expand(&md5res[0],text); s = md5_init(); md5_process_bytes(s,plain,strlen(plain)); md5_process_bytes(s,text,20); md5_result(s,&md5res[0]); base_expand(&md5res[0],text+20); text[40] = '\0'; return(text); }