/** * Generate a random string of the specified size * * @param int $size The size of the requested random string * * @return string A string of the requested size */ public function generate($size) { $result = ''; while (Util::safeStrlen($result) < $size) { $result = uniqid($result, true); } return Util::safeSubstr($result, 0, $size); }
/** * Generate a random string of the specified size * * @param int $size The size of the requested random string * * @return string A string of the requested size */ public function generate($size) { $result = ''; $seed = microtime() . memory_get_usage(); self::$state = hash('sha512', self::$state . $seed, true); /** * Make the generated randomness a bit better by forcing a GC run which * should complete in a indeterminate amount of time, hence improving * the strength of the randomness a bit. It's still not crypto-safe, * but at least it's more difficult to predict. */ gc_collect_cycles(); for ($i = 0; $i < $size; $i += 8) { $seed = self::$state . microtime() . pack('Ni', $i, self::counter()); self::$state = hash('sha512', $seed, true); /** * We only use the first 8 bytes here to prevent exposing the state * in its entirety, which could potentially expose other random * generations in the future (in the same process)... */ $result .= Util::safeSubstr(self::$state, 0, 8); } return Util::safeSubstr($result, 0, $size); }
/** * Get the block size (the size of the individual blocks used for the mixing) * * @return int The block size */ protected function getPartSize() { return Util::safeStrlen(hash($this->hash, '', true)); }
private function str_split($string, $size) { $blocks = array(); $length = Util::safeStrlen($string); $parts = ceil($length / $size); for ($i = 0; $i < $parts; $i++) { $blocks[] = Util::safeSubstr($string, $i * $length, $length); } return $blocks; }