PHP 生成 UUID 函数
程序员文章站
2022-05-18 19:29:29
Generate name based md5 UUID (version 3) 函数来源:fzaninotto/faker 地址:https://github.com/fzaninotto/Faker ......
generate name based md5 uuid (version 3)
/** * generate name based md5 uuid (version 3). * @example '7e57d004-2b97-0e7a-b45f-5387367791cd' */ public function uuid() { // fix for compatibility with 32bit architecture; seed range restricted to 62bit $seed = mt_rand(0, 2147483647) . '#' . mt_rand(0, 2147483647); // hash the seed and convert to a byte array $val = md5($seed, true); $byte = array_values(unpack('c16', $val)); // extract fields from byte array $tlo = ($byte[0] << 24) | ($byte[1] << 16) | ($byte[2] << 8) | $byte[3]; $tmi = ($byte[4] << 8) | $byte[5]; $thi = ($byte[6] << 8) | $byte[7]; $cslo = $byte[9]; $cshi = $byte[8] & 0x3f | (1 << 7); // correct byte order for big edian architecture if (pack('l', 0x6162797a) == pack('n', 0x6162797a)) { $tlo = (($tlo & 0x000000ff) << 24) | (($tlo & 0x0000ff00) << 8) | (($tlo & 0x00ff0000) >> 8) | (($tlo & 0xff000000) >> 24); $tmi = (($tmi & 0x00ff) << 8) | (($tmi & 0xff00) >> 8); $thi = (($thi & 0x00ff) << 8) | (($thi & 0xff00) >> 8); } // apply version number $thi &= 0x0fff; $thi |= (3 << 12); // cast to string $uuid = sprintf( '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x', $tlo, $tmi, $thi, $cshi, $cslo, $byte[10], $byte[11], $byte[12], $byte[13], $byte[14], $byte[15] ); return $uuid; }
函数来源:fzaninotto/faker
上一篇: 模拟登陆 12306网站