生成UUID/GUID

php的生成办法:


Name = $_ENV["COMPUTERNAME"];
$address->IP = $_SERVER["SERVER_ADDR"];

return $address;
}

function toString()
{
return strtolower($this->Name.'/'.$this->IP);
}

}

class Random
{
function nextLong()
{
$tmp = rand(0,1)?'-':'';
return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
}
}

// 三段
// 一段是微秒 一段是地址 一段是随机数
class Guid
{

var $valueBeforeMD5;
var $valueAfterMD5;

function Guid()
{
$this->getGuid();
}
//
function getGuid()
{
$address = NetAddress::getLocalHost();
$this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();
$this->valueAfterMD5 = md5($this->valueBeforeMD5);
}

function newGuid()
{
$Guid = new Guid();
return $Guid;
}

function toString()
{
$raw = strtoupper($this->valueAfterMD5);
return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
}

}
?>

然后就可以这样调用:

1. toString();
5. ?>

linux下可以用uuidgen命令来生成UUID,也可以在c/c++程序里调用:

/* clear.c */
void uuid_clear(uuid_t uu);

/* compare.c */
int uuid_compare(const uuid_t uu1, const uuid_t uu2);

/* copy.c */
void uuid_copy(uuid_t dst, const uuid_t src);

/* gen_uuid.c */
void uuid_generate(uuid_t out);
void uuid_generate_random(uuid_t out);
void uuid_generate_time(uuid_t out);

/* isnull.c */
int uuid_is_null(const uuid_t uu);

/* parse.c */
int uuid_parse(const char *in, uuid_t uu);

/* unparse.c */
void uuid_unparse(const uuid_t uu, char *out);

/* uuid_time.c */
time_t uuid_time(const uuid_t uu, struct timeval *ret_tv);
int uuid_type(const uuid_t uu);
int uuid_variant(const uuid_t uu);

在ubuntu下,需要确认已经安装对应的库:

sudo aptitude install libuuid1 uuid-dev


已发布

分类

来自

标签:

评论

《“生成UUID/GUID”》 有 1 条评论

  1. gotolab 的头像
    gotolab

    hello,最近我在找人帮我将web-wiz(8.03版)转换为phpbb,看了你的blog,发现你已经实现了,另有1个小工具需要开发,不知道你这边有无兴趣,请联系我msn:gotolab@hotmail.com或者email到该地址也行

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注