Generate UUID v5

Generate a name-based UUID v5 using SHA-1 hashing of namespace and name.

Code

Utilities
$nsHex = str_replace('-', '', $namespace);
$nsBytes = pack('H*', $nsHex);
$hash = sha1($nsBytes . $name, true);
$hash = substr($hash, 0, 16);
$hash[6] = chr((ord($hash[6]) & 0x0f) | 0x50);
$hash[8] = chr((ord($hash[8]) & 0x3f) | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($hash), 4));

Parameters

Namespace UUID (use DNS, URL, OID, or X500 namespace).

Name to hash within the namespace.

Server

More PHP Snippets