From 900406c359c319100c892a38be2bb5d76a8f85b9 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Tue, 12 Dec 2006 14:38:03 +0000 Subject: passwd: shrink by ~10 bytes, use PRNG instead of usleep. --- loginutils/passwd.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/loginutils/passwd.c b/loginutils/passwd.c index 1f488290a..a062596b4 100644 --- a/loginutils/passwd.c +++ b/loginutils/passwd.c @@ -5,7 +5,6 @@ #include "busybox.h" #include -#include /* times() */ static void nuke_str(char *str) @@ -16,7 +15,8 @@ static void nuke_str(char *str) static int i64c(int i) { - if (i <= 0) + i &= 0x3f; + if (i == 0) return '.'; if (i == 1) return '/'; @@ -30,23 +30,16 @@ static int i64c(int i) static void crypt_make_salt(char *p, int cnt) { -#if !defined(__GLIBC__) - struct tms t; -#define TIMES times(&t) -#else -/* glibc allows for times(NULL) a-la time() */ -#define TIMES times(NULL) -#endif - unsigned long x = x; /* it's pointless to initialize it anyway :) */ + unsigned x = x; /* it's pointless to initialize it anyway :) */ - x += getpid(); + x += getpid() + time(NULL) + clock(); do { - /* clock() and times() variability is different between systems */ - /* hopefully at least one is good enough */ - x += time(NULL) + clock() + TIMES; - *p++ = i64c(((x >> 18) ^ (x >> 6)) & 0x3f); - *p++ = i64c(((x >> 12) ^ x) & 0x3f); - usleep(100); /* or else time() etc won't change */ + /* x = (x*1664525 + 1013904223) mod 2^32 generator is lame + * (low-order bit is not "random", etc...), + * but for our purposes it is good enough */ + x = x*1664525 + 1013904223; + *p++ = i64c(x >> 16); + *p++ = i64c(x >> 22); } while (--cnt); *p = '\0'; } -- cgit v1.2.3