创建具有设定前缀的 GUID

发布于 2024-07-09 16:27:42 字数 178 浏览 2 评论 0原文

我想知道是否有一种方法可以生成有效的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

╄→承喏 2024-07-16 16:27:42

抱歉,您对 GUID 的要求太多了。 从您的问题和您自己的答案/更新中总结,您希望它

  • 1 是一个 GUID
  • 2 不与任何其他 GUID 冲突(全局唯一)
  • 3 忽略第一位解释的标准,使用保留值
  • 4 使用剩余位的个人方案

这是不可能的,证明:
如果可能的话,我可以生成一个 GUID G1,您可以生成另一个 GUID G2。 由于我们都忽略标准并使用相同的保留前缀,并且我个人对其他位的方案超出了您的控制范围,因此我的 GUID G1 可能会与您的 GUID G2 发生冲突。 GUID 的非冲突特性源于坚持 GUID 标准。

防止冲突的机制本质上确实是隐私敏感的。 如果我随机生成 GUID G1,如果满足两个条件,我可以保证随机 GUID 是唯一的:

  • 1 它是我控制下的 GUID 子集的成员,
  • 2 我之前没有生成 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

  • 1 be a GUID
  • 2 not collide with any other GUID (be globally unique)
  • 3 Ignore the standard on the interpretation of the first bits, using a reserved value
  • 4 Use a personal scheme for the remaining bits

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:

  • 1 It's a member of the subset of GUIDs under my control and
  • 2 I didn't generate the GUID before.

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.

悲念泪 2024-07-16 16:27:42

嗯...所以,您基本上喜欢 12 字节 GUID? 因为,一旦删除前 4 个字节(您的 AAA)的唯一性,您就拥有 破坏了现有算法 - 您需要提出自己的算法。

根据相关的 RFC,GUID 格式分解为:

  UUID                   = time-low "-" time-mid "-"
                           time-high-and-version "-"
                           clock-seq-and-reserved
                           clock-seq-low "-" node
  time-low               = 4hexOctet
  time-mid               = 2hexOctet
  time-high-and-version  = 2hexOctet
  clock-seq-and-reserved = hexOctet
  clock-seq-low          = hexOctet
  node                   = 6hexOctet
  hexOctet               = hexDigit hexDigit
  hexDigit =
        "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
        "a" / "b" / "c" / "d" / "e" / "f" /
        "A" / "B" / "C" / "D" / "E" / "F"

那里唯一的静态数据是版本(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:

  UUID                   = time-low "-" time-mid "-"
                           time-high-and-version "-"
                           clock-seq-and-reserved
                           clock-seq-low "-" node
  time-low               = 4hexOctet
  time-mid               = 2hexOctet
  time-high-and-version  = 2hexOctet
  clock-seq-and-reserved = hexOctet
  clock-seq-low          = hexOctet
  node                   = 6hexOctet
  hexOctet               = hexDigit hexDigit
  hexDigit =
        "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
        "a" / "b" / "c" / "d" / "e" / "f" /
        "A" / "B" / "C" / "D" / "E" / "F"

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.

伴我心暖 2024-07-16 16:27:42

无法创建 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...

时光清浅 2024-07-16 16:27:42

我最近有一个类似的需求 - 我需要一个 GUID:

  • 由标准 guid 算法创建,因此有可能是全局唯一的,
  • 具有定义的前缀。

正如你可能想象的那样,我做了一些我不应该做的事情。

您在其中一条评论中提到,您可以让 GUID 生成器运行,直到它碰巧遇到具有您需要的前缀的 GUID。 这就是我采取的策略。 代码如下:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string target_prefix = "dead";

            while (true)
            {
                Guid g = Guid.NewGuid();
                string gs = g.ToString();
                if (gs.Substring(0, target_prefix.Length) == target_prefix)
                {
                    Console.WriteLine("Match: " + gs);
                }
                else
                {
                    //Console.WriteLine("Mismatch: " + gs);
                }
            }
        }
    }
}

对于较小的前缀,它会更快地生成匹配项。 我敢打赌,目标前缀的每个数字的长度都是 16 倍。

I recently had a similar need - I needed a GUID that was:

  • created by the standard guid algorithms, and therefore has a chance of being globally unique
  • has a defined prefix.

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:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string target_prefix = "dead";

            while (true)
            {
                Guid g = Guid.NewGuid();
                string gs = g.ToString();
                if (gs.Substring(0, target_prefix.Length) == target_prefix)
                {
                    Console.WriteLine("Match: " + gs);
                }
                else
                {
                    //Console.WriteLine("Mismatch: " + gs);
                }
            }
        }
    }
}

For smaller prefixes it produces matches more quickly. I bet it's 16x as long for every digit of target prefix.

迷荒 2024-07-16 16:27:42

您可以简单地创建一个 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.

白色秋天 2024-07-16 16:27:42

谢谢。 我对这些尝试的问题是,它们不能保证全局唯一,如 Raymond Chen 指出。 我想知道是否有另一种算法可以生成唯一的 GUID。 我记得曾经有使用时间戳和/或 NIC MAC 地址的实现,但它们不再使用,因为它们的加密性不强和/或存在隐私问题。

我想:如果我自己编的话应该没问题吧? 根据 维基百科

数据 4 中第二个字节的一到三个最高有效位定义 GUID 的类型变体:

模式描述
0 网络计算系统向后兼容性
10 标准
110 Microsoft 组件对象模型向后兼容性; 这包括 IUnknown 和 IDispatch 等重要接口的 GUID。
111 保留供将来使用。

Data3 的最高四位定义版本号和使用的算法。

因此,如果我在 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:

One to three of the most significant bits of the second byte in Data 4 define the type variant of the GUID:

Pattern Description
0 Network Computing System backward compatibility
10 Standard
110 Microsoft Component Object Model backward compatibility; this includes the GUID's for important interfaces like IUnknown and IDispatch.
111 Reserved for future use.

The most significant four bits of Data3 define the version number, and the algorithm used.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文