diff options
author | Denis Vlasenko | 2008-12-07 01:16:34 +0000 |
---|---|---|
committer | Denis Vlasenko | 2008-12-07 01:16:34 +0000 |
commit | d1a84a2880073f6cc5e2f9f4e5f236cd110f01a0 (patch) | |
tree | 8e5b81c863ef3b91870812e822fecc3d97b0aff7 /libbb/pw_encrypt.c | |
parent | db12d1d733ab7de0c5f4cda261eb79fd334a4ed9 (diff) | |
download | busybox-d1a84a2880073f6cc5e2f9f4e5f236cd110f01a0.zip busybox-d1a84a2880073f6cc5e2f9f4e5f236cd110f01a0.tar.gz |
libbb: move crypt_make_salt() to pw_encrypt.c, reuse
bin-to-ascii64 conversion which does not require an array.
function old new delta
to64 29 33 +4
to64_msb_first 63 62 -1
ascii64 65 - -65
Diffstat (limited to 'libbb/pw_encrypt.c')
-rw-r--r-- | libbb/pw_encrypt.c | 55 |
1 files changed, 46 insertions, 9 deletions
diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c index 572591e..6fc0ba8 100644 --- a/libbb/pw_encrypt.c +++ b/libbb/pw_encrypt.c @@ -1,6 +1,6 @@ /* vi: set sw=4 ts=4: */ /* - * Utility routine. + * Utility routines. * * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> * @@ -9,25 +9,62 @@ #include "libbb.h" -#if ENABLE_USE_BB_CRYPT - -/* - * DES and MD5 crypt implementations are taken from uclibc. - * They were modified to not use static buffers. +/* static const uint8_t ascii64[] = + * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; */ -/* Used by pw_encrypt_XXX.c */ -static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +static int i64c(int i) +{ + i &= 0x3f; + if (i == 0) + return '.'; + if (i == 1) + return '/'; + if (i < 12) + return ('0' - 2 + i); + if (i < 38) + return ('A' - 12 + i); + return ('a' - 38 + i); +} + +int FAST_FUNC crypt_make_salt(char *p, int cnt, int x) +{ + x += getpid() + time(NULL); + do { + /* x = (x*1664525 + 1013904223) % 2^32 generator is lame + * (low-order bit is not "random", etc...), + * but for our purposes it is good enough */ + x = x*1664525 + 1013904223; + /* BTW, Park and Miller's "minimal standard generator" is + * x = x*16807 % ((2^31)-1) + * It has no problem with visibly alternating lowest bit + * but is also weak in cryptographic sense + needs div, + * which needs more code (and slower) on many CPUs */ + *p++ = i64c(x >> 16); + *p++ = i64c(x >> 22); + } while (--cnt); + *p = '\0'; + return x; +} + +#if ENABLE_USE_BB_CRYPT + static char* to64(char *s, unsigned v, int n) { while (--n >= 0) { - *s++ = ascii64[v & 0x3f]; + /* *s++ = ascii64[v & 0x3f]; */ + *s++ = i64c(v); v >>= 6; } return s; } +/* + * DES and MD5 crypt implementations are taken from uclibc. + * They were modified to not use static buffers. + */ + #include "pw_encrypt_des.c" #include "pw_encrypt_md5.c" #if ENABLE_USE_BB_CRYPT_SHA |