[wp-trac] [WordPress Trac] #6492: Guids No Longer Have Permalink Format
WordPress Trac
wp-trac at lists.automattic.com
Thu Sep 2 21:53:22 UTC 2010
#6492: Guids No Longer Have Permalink Format
-------------------------------------+--------------------------------------
Reporter: brianwhite | Owner: anonymous
Type: enhancement | Status: reopened
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 2.5
Severity: trivial | Resolution:
Keywords: has-patch guid featured |
-------------------------------------+--------------------------------------
Comment(by Denis-de-Bernardy):
here's a php implementation for v4 uuids in PHP 5.3, in case it helps:
{{{
/**
* Generates random bytes for use in UUIDs and password salts,
using
* (when available) a cryptographically strong random number
generator.
*
* @param integer $bytes The number of random bytes to generate
* @return string Random bytes
*/
public static function random($bytes) {
$source = static::$_source ?: static::_source();
return $source($bytes);
}
/**
* Initializes Crypto::$_source using the best available random
* number generator.
*
* When available, /dev/urandom and COM gets used on *unix and
* Windows systems, respectively.
*
* If all else fails, a Mersenne Twister gets used. (Strictly
* speaking, this fallback is inadequate, but good enough.)
*
* @return Closure The random number generator.
*/
protected static function _source() {
switch (true) {
case isset(static::$_source);
return static::$_source;
case is_readable('/dev/urandom') && $fp =
fopen('/dev/urandom', 'rb'):
return static::$_source = function($bytes)
use (&$fp) {
return fread($fp, $bytes);
};
case class_exists('COM', 0):
// http://msdn.microsoft.com/en-
us/library/aa388182(VS.85).aspx
try {
$com = new
COM('CAPICOM.Utilities.1');
return static::$_source =
function($bytes) use ($com) {
return
base64_decode($com->GetRandom($bytes,0));
};
} catch (Exception $e) {
}
default:
// fallback to using mt_rand() if all else
fails
return static::$_source = function($bytes)
{
$rand = '';
for ($i = 0; $i < $bytes; $i++) {
$rand .= chr(mt_rand(0,
255));
}
return $rand;
};
}
}
/**
* UUID related constants
*/
const clearVer = 15; // 00001111 Clears all bits of version byte
const version4 = 64; // 01000000 Sets the version bit
const clearVar = 63; // 00111111 Clears relevant bits of variant
byte
const varRFC = 128; // 10000000 The RFC 4122 variant
/**
* Generates an RFC 4122-compliant version 4 UUID.
*
* @return string The string representation of an RFC
4122-compliant, version 4 UUID.
* @link http://www.ietf.org/rfc/rfc4122.txt
*/
public static function uuid() {
$uuid = Crypto::random(16);
// Set version
$uuid[6] = chr(ord($uuid[6]) & static::clearVer |
static::version4);
// Set variant
$uuid[8] = chr(ord($uuid[8]) & static::clearVar |
static::varRFC);
// Return the uuid's string representation
return bin2hex(substr($uuid, 0, 4)) . '-'
. bin2hex(substr($uuid, 4, 2)) . '-'
. bin2hex(substr($uuid, 6, 2)) . '-'
. bin2hex(substr($uuid, 8, 2)) . '-'
. bin2hex(substr($uuid, 10, 6));
}
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/6492#comment:47>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list