crypt_make_salt.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* 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. #include "libbb.h"
  11. static int i64c(int i)
  12. {
  13. i &= 0x3f;
  14. if (i == 0)
  15. return '.';
  16. if (i == 1)
  17. return '/';
  18. if (i < 12)
  19. return ('0' - 2 + i);
  20. if (i < 38)
  21. return ('A' - 12 + i);
  22. return ('a' - 38 + i);
  23. }
  24. int crypt_make_salt(char *p, int cnt, int x)
  25. {
  26. x += getpid() + time(NULL);
  27. do {
  28. /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
  29. * (low-order bit is not "random", etc...),
  30. * but for our purposes it is good enough */
  31. x = x*1664525 + 1013904223;
  32. /* BTW, Park and Miller's "minimal standard generator" is
  33. * x = x*16807 % ((2^31)-1)
  34. * It has no problem with visibly alternating lowest bit
  35. * but is also weak in cryptographic sense + needs div,
  36. * which needs more code (and slower) on many CPUs */
  37. *p++ = i64c(x >> 16);
  38. *p++ = i64c(x >> 22);
  39. } while (--cnt);
  40. *p = '\0';
  41. return x;
  42. }