fork download
  1. In theory, this salt encodes 64 bits of entropy. In practice, the values of
  2. time() and getmypid() are almost predictable and thus very bad choices for
  3. seeding. But let's pretend they are just fine.
  4.  
  5. <?php
  6.  
  7. $seed1 = time(); // quite bad choice of seed
  8. $seed2 = getmypid(); // EVEN WORSE choice of seed (read the manual)
  9. $random = '';
  10.  
  11. mt_srand($seed1);
  12. $random .= random_bits(32);
  13.  
  14. mt_srand($seed2);
  15. $random .= random_bits(32);
  16.  
  17. echo $random;
  18.  
  19. // Counts how many bits are needed to represent $value
  20. function count_bits($value) {
  21. for($count = 0; $value != 0; $value >>= 1) {
  22. ++$count;
  23. }
  24. return $count;
  25. }
  26.  
  27. // Returns a base16 random string of at least $bits bits
  28. // Actual bits returned will be a multiple of 4 (1 hex digit)
  29. function random_bits($bits) {
  30. $result = '';
  31. $accumulated_bits = 0;
  32. $total_bits = count_bits(mt_getrandmax());
  33. $usable_bits = intval($total_bits / 8) * 8;
  34.  
  35. while ($accumulated_bits < $bits) {
  36. $bits_to_add = min($total_bits - $usable_bits, $bits - $accumulated_bits);
  37. if ($bits_to_add % 4 != 0) {
  38. // add bits in whole increments of 4
  39. $bits_to_add += 4 - $bits_to_add % 4;
  40. }
  41.  
  42. // isolate leftmost $bits_to_add from mt_rand() result
  43. $more_bits = mt_rand() & ((1 << $bits_to_add) - 1);
  44.  
  45. // format as hex (this will be safe)
  46. $format_string = '%0'.($bits_to_add / 4).'x';
  47. $result .= sprintf($format_string, $more_bits);
  48. $accumulated_bits += $bits_to_add;
  49. }
  50.  
  51. return $result;
  52. }
  53.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
In theory, this salt encodes 64 bits of entropy. In practice, the values of
time() and getmypid() are almost predictable and thus very bad choices for
seeding. But let's pretend they are just fine.

a77915f8393f66d3