blob: 393eba5f9bd1ecff1c918ebcba18a480dc98bbe8 [file] [log] [blame]
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +00001/* vi: set sw=4 ts=4: */
2/*
3 * crypt_make_salt
4 *
5 * i64c was also put here, this is the only function that uses it.
6 *
7 * Lifted from loginutils/passwd.c by Thomas Lundquist <thomasez@zelow.no>
8 *
9 */
10
11#include "libbb.h"
12
13static int i64c(int i)
14{
15 i &= 0x3f;
16 if (i == 0)
17 return '.';
18 if (i == 1)
19 return '/';
20 if (i < 12)
21 return ('0' - 2 + i);
22 if (i < 38)
23 return ('A' - 12 + i);
24 return ('a' - 38 + i);
25}
26
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000027int FAST_FUNC crypt_make_salt(char *p, int cnt, int x)
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000028{
Denis Vlasenko21d10142007-07-20 21:28:41 +000029 x += getpid() + time(NULL);
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000030 do {
31 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
32 * (low-order bit is not "random", etc...),
33 * but for our purposes it is good enough */
34 x = x*1664525 + 1013904223;
35 /* BTW, Park and Miller's "minimal standard generator" is
36 * x = x*16807 % ((2^31)-1)
37 * It has no problem with visibly alternating lowest bit
38 * but is also weak in cryptographic sense + needs div,
39 * which needs more code (and slower) on many CPUs */
40 *p++ = i64c(x >> 16);
41 *p++ = i64c(x >> 22);
42 } while (--cnt);
43 *p = '\0';
Denis Vlasenko21d10142007-07-20 21:28:41 +000044 return x;
Denis Vlasenkoa035e9f2007-05-08 23:23:35 +000045}