1
0
forked from bip/bip

ensure null terminated str

This commit is contained in:
Arnaud Cornet 2008-12-29 14:04:51 +01:00
parent 5a5f1b8bfb
commit a4b101cea8

View File

@ -33,23 +33,23 @@ void readpass(char *buffer, int buflen)
fprintf(stderr, "Unable to open tty: %s\n", strerror(errno)); fprintf(stderr, "Unable to open tty: %s\n", strerror(errno));
exit(1); exit(1);
} }
struct termios tt, ttback; struct termios tt, ttback;
memset(&ttback, 0, sizeof(ttback)); memset(&ttback, 0, sizeof(ttback));
if (tcgetattr(ttyfd, &ttback) < 0) { if (tcgetattr(ttyfd, &ttback) < 0) {
printf("tcgetattr failed: %s\n", strerror(errno)); fprintf(stderr, "tcgetattr failed: %s\n", strerror(errno));
exit(1); exit(1);
} }
memcpy(&tt, &ttback, sizeof(ttback)); memcpy(&tt, &ttback, sizeof(ttback));
tt.c_lflag &= ~(ICANON|ECHO); tt.c_lflag &= ~(ICANON|ECHO);
if (tcsetattr(ttyfd, TCSANOW, &tt) < 0) { if (tcsetattr(ttyfd, TCSANOW, &tt) < 0) {
printf("tcsetattr failed: %s\n", strerror(errno)); fprintf(stderr, "tcsetattr failed: %s\n", strerror(errno));
exit(1); exit(1);
} }
write(ttyfd, "Password: ", 10); write(ttyfd, "Password: ", 10);
int idx = 0; int idx = 0;
while (idx < buflen) { while (idx < buflen) {
read(ttyfd, buffer+idx, 1); read(ttyfd, buffer+idx, 1);
@ -59,9 +59,9 @@ void readpass(char *buffer, int buflen)
} }
idx++; idx++;
} }
write(ttyfd, "\n", 1); write(ttyfd, "\n", 1);
tcsetattr(ttyfd, TCSANOW, &ttback); tcsetattr(ttyfd, TCSANOW, &ttback);
close(ttyfd); close(ttyfd);
} }
@ -74,11 +74,12 @@ int main(void)
unsigned int seed; unsigned int seed;
readpass(str, 256); readpass(str, 256);
str[255] = 0;
// the time used to type the pass is entropy // the time used to type the pass is entropy
srand(time(NULL)); srand(time(NULL));
seed = rand(); seed = rand();
md5 = chash_double(str, seed); md5 = chash_double(str, seed);
for (i = 0; i < 20; i++) for (i = 0; i < 20; i++)
printf("%02x", md5[i]); printf("%02x", md5[i]);