/** * Generates a string of binary data of the specified length * * @param integer $length The number of bytes of random binary data to generate * @return string A binary string */ public function generate($length) { if ($length < self::TIMESTAMP_BYTES || $length < 0) { throw new \InvalidArgumentException('Length must be a positive integer.'); } $hash = ''; if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) { $hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES); } $lsbTime = str_pad($this->converter->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT); return hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime); }
/** * Generates a string of binary data of the specified length * * @param integer $length The number of bytes of random binary data to generate * @return string A binary string */ public function generate($length) { if ($length < $this->timestampBytes || $length < 0) { throw new \InvalidArgumentException('Length must be a positive integer.'); } $hash = ''; if ($this->timestampBytes > 0 && $length > $this->timestampBytes) { $hash = $this->randomGenerator->generate($length - $this->timestampBytes); } $lsbTime = str_pad($this->converter->toHex($this->timestamp()), $this->timestampBytes * 2, '0', STR_PAD_LEFT); if ($this->timestampBytes > 0 && strlen($lsbTime) > $this->timestampBytes * 2) { $lsbTime = substr($lsbTime, 0 - $this->timestampBytes * 2); } return hex2bin(str_pad(bin2hex($hash), $length - $this->timestampBytes, '0')) . hex2bin($lsbTime); }
public function uuid4() { $bytes = $this->randomGenerator->generate(16); // When converting the bytes to hex, it turns into a 32-character // hexadecimal string that looks a lot like an MD5 hash, so at this // point, we can just pass it to uuidFromHashedName. $hex = bin2hex($bytes); return $this->uuidFromHashedName($hex, 4); }