You are here

function uuid_generate_v5 in Universally Unique IDentifier 7

Generates a version 5 compliant UUID.

Code lifted from PHP manual comment by Andrew Moore.

Parameters

string $namespace: Namespace UUID as a hex encoded string.

string $name: The name for the generating the UUID.

Return value

string UUID as a hex encoded string.

1 call to uuid_generate_v5()
UUIDV5TestCase::testV5Function in ./uuid.test
Tests uuid function calls.

File

./uuid.inc, line 54
Handling of universally unique identifiers.

Code

function uuid_generate_v5($namespace, $name) {
  if (!uuid_is_valid($namespace)) {
    return FALSE;
  }

  // Get hexadecimal components of namespace.
  $nhex = str_replace(array(
    '-',
    '{',
    '}',
  ), '', $namespace);

  // Binary Value.
  $nstr = '';

  // Convert Namespace UUID to bits.
  for ($i = 0; $i < strlen($nhex); $i += 2) {
    $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1]));
  }

  // Calculate hash value.
  $hash = sha1($nstr . $name);
  return sprintf('%08s-%04s-%04x-%04x-%12s', substr($hash, 0, 8), substr($hash, 8, 4), hexdec(substr($hash, 12, 4)) & 0xfff | 0x5000, hexdec(substr($hash, 16, 4)) & 0x3fff | 0x8000, substr($hash, 20, 12));
}