创建具有设定前缀的 GUID
我想知道是否有一种方法可以生成有效的 GUID/UUID,其中第一个(或任何部分)部分是用户选择的前缀。
即,GUID 的格式为 AAAAAAAA-BBBB-CCCC-DDDD-DDDDDDDDDDDD,我想将任何部分设置为预定义值(最好是 AAA)。 目标是让 GUID 仍然是全球唯一的,但它们不需要是加密安全的。
i wonder if there is a way to generate valid GUIDs/UUIDs where the first (or any part) part is a user-selected prefix.
I.e., the GUID has the format AAAAAAAA-BBBB-CCCC-DDDD-DDDDDDDDDDDD, and I want to set any part to a pre-defined value (ideally the AAA's). The goal is to have GUIDs still globally unique, but they do not need to be cryptographically safe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
抱歉,您对 GUID 的要求太多了。 从您的问题和您自己的答案/更新中总结,您希望它
这是不可能的,证明:
如果可能的话,我可以生成一个 GUID G1,您可以生成另一个 GUID G2。 由于我们都忽略标准并使用相同的保留前缀,并且我个人对其他位的方案超出了您的控制范围,因此我的 GUID G1 可能会与您的 GUID G2 发生冲突。 GUID 的非冲突特性源于坚持 GUID 标准。
防止冲突的机制本质上确实是隐私敏感的。 如果我随机生成 GUID G1,如果满足两个条件,我可以保证随机 GUID 是唯一的:
对于您控制下的子集之外的 GUID,您无法保证 (2)。 但是如何将不重叠的 GUID 子集分配给单个人呢? 使用网卡的MAC是一种简单、有效的方法。 其他方式也是可能的。 但无论如何,这样一个子集的存在本身就涉及隐私。 它必须属于某人,而我必须能够确定那是我还是别人。 证明两个随机 GUID G1 和 G2 是否属于同一子集(即人)有点困难,但当前的方案(您反对的)不会试图隐藏这一点。
Sorry, you want too much from a GUID. Summarizing from both your question and your own answer/update, you want it to
This is impossible, proof:
If it was possible, I could generate a GUID G1 and you could generate another GUID G2. Since we both ignore the standard and use the same reserved prefix, and my personal scheme for the other bits is outside your control, my GUID G1 can clash with your GUID G2. The non-collision propery of GUIDs follows from sticking to the GUID standard.
The mechanisms to prevent collisions are indeed inherently privacy-sensitive. If I generate at random a GUID G1, I can guarantee that random GUID is unique if two conditions are satisfied:
For GUIDs outside the subset under your control, you cannot guarantee (2). But how do you assign non-overlapping subsets of GUIDs to a single person? Using the MAC of a NIC is a simple, effective way. Other means are also possible. But in any case, the mere existence of such a subset is privacy-implicating. It's got to belong to someone, and I must be able to determine whether that's me or someone else. It's a bit harder to prove whether two random GUIDs G1 and G2 belong to the same subset (ie. person) but the current schemes (which you object to) do not try to hide that.
嗯...所以,您基本上喜欢 12 字节 GUID? 因为,一旦删除前 4 个字节(您的 AAA)的唯一性,您就拥有 破坏了现有算法 - 您需要提出自己的算法。
根据相关的 RFC,GUID 格式分解为:
那里唯一的静态数据是版本(4 位)和保留/变体(2-3 位)。 我不认为他们允许任何“用户指定”版本,但我想说,如果您使用 1111 作为版本标识符,在可预见的将来您将是安全的。 现有版本位于第 4.1.3 节中,但到目前为止只定义了 5 个……这在冲突之前为您提供了 11 个修订版本。
因此,如果您可以接受 6 或 7 位的清晰度,则可以使用 Guid.NewGuid().ToByteArray() 并创建 新的 Guid 经过你的一番摆弄后应该可以让你到达那里。
Hmmm...so, you'd basically like a 12 byte GUID? Since, once you remove the uniqueness of the first 4 bytes (your AAA's), you've broken the existing algorithm - you'll need to come up with your own algorithm.
According to the relevant RFC, the GUID format breaks down to:
The only static data in there is version (4 bits) and reserved/variant (2-3 bits). I don't see that they allowed for any "user specified" versions, but I'd say you'll be safe for the foreseeable future if you use 1111 as your version identifier. The existing versions are in section 4.1.3, but only 5 have been defined so far...that gives you 11 more revisions before collision.
So, if you can live with 6 or 7 bits of distinctness, a combination of Guid.NewGuid().ToByteArray() and creating a new Guid after your bit fiddling should get you there.
无法创建 GUID/UUID,其中第一个(或任何部分)部分是用户选择的前缀,而您可以编写自己的函数来创建具有相同数量 (36/38) 字符的唯一 id...
Not possible to create GUIDs/UUIDs where the first (or any part) part is a user-selected prefix , whereas you can write your own function to create a unique id wid same number (36/38) of characters...
我最近有一个类似的需求 - 我需要一个 GUID:
正如你可能想象的那样,我做了一些我不应该做的事情。
您在其中一条评论中提到,您可以让 GUID 生成器运行,直到它碰巧遇到具有您需要的前缀的 GUID。 这就是我采取的策略。 代码如下:
对于较小的前缀,它会更快地生成匹配项。 我敢打赌,目标前缀的每个数字的长度都是 16 倍。
I recently had a similar need - I needed a GUID that was:
As you might imagine, I was doing something I shouldn't have.
You mention in one of your comments that you could just let the GUID generator run until it happens to hit upon a guid with the prefix you need. That's the tactic I took. Here's the code:
For smaller prefixes it produces matches more quickly. I bet it's 16x as long for every digit of target prefix.
您可以简单地创建一个 Guid,并将前缀更改为您希望的样子。
在一个操作系统项目中看到过这一点,其中抛出了同样的问题,并通过生成如此多的引导直到一个匹配所需的前缀(呃!)来解决。
<代码>
Guid g = Guid.NewGuid();
字符串 gs = g.ToString();
Guid f = new Guid(string.Format("{0}-{1}", "AAAAAAAA", gs.Substring(gs.IndexOf('-') + 1)));
不太
好,但是可以。
这个主题的其他帖子让我困扰的是,指南应该是全球唯一的,这在所有情况下都是错误的,它有足够的空间来生成唯一的指南,但不能保证全球唯一。 在生成指南时甚至不考虑时间。
You can simply create a Guid, and change the prefix to be like you whish it to be.
Have seen this in an OS-Project, where same question was thrown and solved by generating so many guids until one matches the wished prefix (ugh!).
Guid g = Guid.NewGuid();
string gs = g.ToString();
Guid f = new Guid(string.Format("{0}-{1}", "AAAAAAAA", gs.Substring(gs.IndexOf('-') + 1)));
Not nice, but works.
What bothered me from other posts in this subject is, that a guid shall be globally unique, thats wrong in all cases, it has just enough room to generaty unique guids, but nothing guaranteed for global uniquely. Even time is not considered in generating a guid.
谢谢。 我对这些尝试的问题是,它们不能保证全局唯一,如 Raymond Chen 指出。 我想知道是否有另一种算法可以生成唯一的 GUID。 我记得曾经有使用时间戳和/或 NIC MAC 地址的实现,但它们不再使用,因为它们的加密性不强和/或存在隐私问题。
我想:如果我自己编的话应该没问题吧? 根据 维基百科:
因此,如果我在 Data3/Data4 中编写一些内容,我通常会创建自己的实现,该实现不应与任何其他 GUID 冲突,但当然总是存在一些与之相关的风险,因此在这样做之前我想检查一下是否有旧的/不再使用的算法可以生成真正的唯一 ID。
Thanks. My problem with these attempts is that they are not guaranteed to be globally unique, as Raymond Chen pointed out. I was wondering if there is another algorithm that generates GUIDs that are unique. I remember that there used to be implementations that used a Timestamp and/or the NIC MAC Address, but they are not used anymore since they are not cryptographic strong and/or there were privacy concerns.
I wonder: If I just make up my own, i should be fine? According to Wikipedia:
So if I make up something in Data3/Data4, i would normally create my own implementation that should not clash with any other GUID, but of course there is always a bit of risk associated with that, so before I do that I wanted to check if there is an older/not anymore used algorhithm that generates true Unique Ids.