您现在的位置是:首页 > 电脑 > 

如何在C中生成一个GUID?(How to generate a GUID in C?)

2025-07-28 10:17:59
如何在C中生成一个GUID?(How to generate a GUID in C?) 我想要生成guid来插入到SQLite数据库(即不支持db本身)。 但是,我想控制某些属性: 生成增加的guid值的有序性。 计算机独立。 数据库是公开的,可能/可能不希望GUID允许某人将数据追溯回特定机器。 '足够'的随机性。 guid是db
如何在C中生成一个GUID?(How to generate a GUID in C?)

我想要生成guid来插入到SQLite数据库(即不支持db本身)。 但是,我想控制某些属性:

生成增加的guid值的有序性。 计算机独立。 数据库是公开的,可能/可能不希望GUID允许某人将数据追溯回特定机器。 '足够'的随机性。 guid是db中的关键,它将与许多其他dbs合并,并且可能会变得非常大,这意味着像许多算法那样伪造guid是没有用的。 我可以处理使用特定于系统的API,但请链接Windows和Linux函数,并且像SQLite之类的东西是首选的,我可以使用其他人编写的代码。 我还希望可以在商业应用程序中使用的代码。

I want to generate guids to insert into a SQLite database ( no support from the db itself). However, I would like to have control over certain properties:

Orderedness for generating increasing guid values. Computer independence. The db is public and may/may not want guids to allow someone to trace data back to a specific machine. 'Enough' randomness. The guids are keys in the db which is going to be merged with a lot of other dbs and can get quite large, meaning that faking a guid like many algorithms do is no good. I can deal with using system specific APIs but please link both Windows and Linux functi, and something like SQLite is preferred, where I can just use code someone else wrote. I would also prefer code which is OK to use in a commercial application.

最满意答案

您可以使用或查看Boost.Uuid的代码:

http://www./doc/libs/1_47_0/libs/uuid/index.html

它是一个C ++库,但仍然可以在代码作者的内部到如何在多个系统上检索Uuid。 上次我检查(2010年1月),我发现至少以下Windows和Linux / Solaris的实现(此信息可能已过时):

Linux / Solaris上的UUID / GUID

打开文件到/dev/urandom并读取足够的字节(16)以组成GUID / UUID。

Windows上的UUID / GUID

使用以下WinAPI函数

CryptAcquireContext获取随机上下文 CryptReleaseContext释放获取的随机上下文 CryptGenRandom生成足够的字节来组成一个GUID / UUID

其他实现

在GUID / UUID上的页面有一个您可以使用/学习的替代实施列表:

https://en./wiki/UUID#Implementati

关于你的条件

有一个GUID / UUID类型始终是随机的( 版本4 ),这意味着要与其他GUID / UUID语义兼容,您应该尊重它。

现在,您需要及时订购GUID / UUID。 在不削弱GUID / UUID随机性的情况下,唯一的方法就是将16字节的GUID / UUID作为无符号整数的前缀(这将使您的标识符数据为20字节或更多,具体取决于您的整数)。 只需生成一个GUID / UUID,然后增加整数。

You can either use or look at the code of Boost.Uuid :

http://www./doc/libs/1_47_0/libs/uuid/index.html

It is a C++ library, but still, you can find inside how the code author retrieved the Uuid on multiple systems. Last time I checked (january 2010), I found at least the following implementati for Windows and Linux/Solaris (this info could be outdated):

UUID/GUID on Linux/Solaris

Open a file to /dev/urandom and read enough bytes (16) to make up a GUID/UUID.

UUID/GUID on Windows

Use the following WinAPI functi

CryptAcquireContext to acquire a random context CryptReleaseContext to release the acquired random context CryptGenRandom to generate enough bytes to make up a GUID/UUID

Other implementati

The Wikipedia page on GUID/UUID has a list of alternative implementati you could use/study:

https://en./wiki/UUID#Implementati

About your conditi

There is a GUID/UUID type which is always random (the version 4), meaning that to be compatible with other GUID/UUID semantics, you should respect that.

ow, you want the GUID/UUID to be ordered in time. The only way to do that without weakening the GUID/UUID randomness would be to prefix the 16-byte GUID/UUID with an unsigned integer (which would make your identifier data 20-bytes, or more, depending on your integer). Just generate a GUID/UUID, and increase the integer.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/65555.html

相关标签:无
上传时间: 2023-04-22 19:31:11
留言与评论(共有 19 条评论)
本站网友 什么叫股权激励
11分钟前 发表
//en./wiki/UUID#Implementati About your conditi There is a GUID/UUID type which is always random (the version 4)
本站网友 红参怎么吃
11分钟前 发表
guid是db中的关键
本站网友 龙江二手车网
17分钟前 发表
我发现至少以下Windows和Linux / Solaris的实现(此信息可能已过时): Linux / Solaris上的UUID / GUID 打开文件到/dev/urandom并读取足够的字节(16)以组成GUID / UUID
本站网友 更换公章
18分钟前 发表
我想控制某些属性: 生成增加的guid值的有序性
本站网友 新一线城市名单
23分钟前 发表
我可以使用其他人编写的代码
本站网友 昆山二中学区房
13分钟前 发表
只需生成一个GUID / UUID
本站网友 遗传所
20分钟前 发表
I found at least the following implementati for Windows and Linux/Solaris (this info could be outdated)
本站网友 北京证券
0秒前 发表
Windows上的UUID / GUID 使用以下WinAPI函数 CryptAcquireContext获取随机上下文 CryptReleaseContext释放获取的随机上下文 CryptGenRandom生成足够的字节来组成一个GUID / UUID 其他实现 在GUID / UUID上的页面有一个您可以使用/学习的替代实施列表: https
本站网友 度来林
9分钟前 发表
meaning that to be compatible with other GUID/UUID semantics
本站网友 中医治疗糖尿病
11分钟前 发表
并且可能会变得非常大
本站网友 毛羽
7分钟前 发表
但是
本站网友 宾馆大厅装修效果图
15分钟前 发表
UUID/GUID on Linux/Solaris Open a file to /dev/urandom and read enough bytes (16) to make up a GUID/UUID. UUID/GUID on Windows Use the following WinAPI functi CryptAcquireContext to acquire a random context CryptReleaseContext to release the acquired random context CryptGenRandom to generate enough bytes to make up a GUID/UUID Other implementati The Wikipedia page on GUID/UUID has a list of alternative implementati you could use/study
本站网友 运20的发动机
29分钟前 发表
在不削弱GUID / UUID随机性的情况下
本站网友 羌活的功效与作用
27分钟前 发表
'足够'的随机性
本站网友 白蚁巢
6分钟前 发表
但是
本站网友 中国电
29分钟前 发表
您应该尊重它
本站网友 爬楼梯减肥
15分钟前 发表
You can either use or look at the code of Boost.Uuid
本站网友 深圳市搜房网
30分钟前 发表
它将与许多其他dbs合并