note 106427 deleted from function.uniqid by danbrown
| From: | danbrown@php.net | Date: | Tue, 15 Nov 2011 16:44:20 +0000 |
| Subject: | note 106427 deleted from function.uniqid by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184496@lists.php.net to get a copy of this message | ||
Note Submitter: previous-sample-removed
----
function uuidgen($sha1 = false, $exec = true) {
$stdout = array();
$cmd = "uuidgen 2>&1";
$status = -100;
$uuid = null;
$haveUUID = false;
if ($exec) {
exec($cmd, $stdout, $status);
}
if ($status == 0 && count($stdout)) {
$uuid = $stdout[0];
if (strlen($uuid) == 36) {
$haveUUID = true;
$uuid = strtoupper($uuid);
}
}
if (!$haveUUID) {
// please use md5($input, true) just an a verbose example
/*
$r = unpack('v*', $md5_raw);
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
$r[1], $r[2], $r[3], $r[4] & 0x0fff | 0x4000,
$r[5] & 0x3fff | 0x8000, $r[6], $r[7], $r[8]);
*/
$md5 = strtoupper(md5(uniqid(microtime(true), true)));
$uuid = sprintf("%04s%04s-%04s-%04s-%04s-%04s%04s%04s",
$md5[0].$md5[1].$md5[2].$md5[3],
$md5[4].$md5[5].$md5[6].$md5[7],
$md5[8].$md5[9].$md5[10].$md5[11],
$md5[12].$md5[13].$md5[14].$md5[15],
$md5[16].$md5[17].$md5[18].$md5[19],
$md5[20].$md5[21].$md5[22].$md5[23],
$md5[24].$md5[25].$md5[26].$md5[27],
$md5[28].$md5[29].$md5[30].$md5[31]
);
}
if ($sha1) {
$uuid = sha1($uuid);
}
return $uuid;
}