如何生成唯一的 64 位密钥

发布于 2024-12-02 06:03:14 字数 418 浏览 4 评论 0原文

我想随机生成唯一的 64 位密钥(伪)来识别模型中的对象。我需要系统所有用户的密钥尽可能唯一(当任意 N 个密钥一起使用时,最大限度地减少冲突的可能性)。

通常的 GUID 目前是不可能的,因为我们的数据很便宜:)。因为我预计在同一上下文中使用的密钥不会超过 100 万个,所以我认为 64 位就足够了(冲突概率约为 10e-7)。

作为旁注,我还需要一个方案来将这些密钥的元组折叠/哈希为单个 64 位密钥,该密钥也需要良好分布/唯一。

因为无论如何我都需要一个好的(分布良好的)散列函数,所以可以将 GUID 对半折叠(也许以某种方式考虑 GUID 中的固定位)吗?还是使用本地 RNG 更好?我如何为 RNG 播种以最大限度地提高生成空间/时间的独特性?我需要多强的 RNG?

我并不是特别追求效率(在某种程度上),但我真的很想确保概率兑现他们的承诺!

I would like to generate unique 64-bit keys (pseudo)-randomly to identify objects in our model. I need the keys to be as unique as possible (minimize the probability of collisions when any N keys are used together) across all users of the system.

Usual GUIDs are out of the question for now because we're data cheap :). Because I don't foresee needing more than 1 million keys used in the same context, I would think 64-bit is enough (probability of collision would be about ~10e-7).

As a side-note, I will also need a scheme to fold/hash tuples of those keys into a single 64-bit key that also needs to be well distributed/unique.

Since I need a good (well distributed) hashing function anyway, would it be ok to fold a GUID in half (maybe accounting for the fixed bits in a GUID in some way)? Or is it better to use a local RNG? How would I seed the RNG to maximize uniqueness across space/time of generation? How strong a RNG would I need?

I'm not particularly looking for efficiency (up to a point) but I'd really like to ensure that the probabilities hold up their promise!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

呆头 2024-12-09 06:03:14

使用快速 128 位加密散列(如 md5)对计数器进行散列,然后分成两部分。这将为您提供“随机”、独立的值,每个值都是 64 位,并且它应该非常高效。

您确定不能使用简单的计数器吗?

更新如果您需要分布式解决方案,只需在每台计算机上放置一个计数器,然后对计算机的 MAC 地址和计数器进行哈希处理即可。为了获得更好的吞吐量,每台机器使用多个计数器,每个计数器具有不同的名称(A、B 等),并对名称进行哈希处理。这是使用哈希的一大优势 - 你可以在其中添加任何内容。只是要小心不要有歧义(例如,在散列的每个内容之间放置“-”,这样名称“1”加上计数“23”就不会与名称“12”和计数相混淆) “3”)。

hash a counter using a fast 128bit cryptographic hash like md5 and then split into two. that will give you "random", independent values that are 64bits each, and it should be pretty efficient.

and are you sure you can't use a simple counter?

update if you need a distributed solution, simply place a counter on each machine and hash the MAC address of the machine plus the counter. for better throughput use multiple counters per machine, each with a different name (A, B etc), and hash the name too. this is the big advantage of using hashes - you can throw anything in there. just be careful not to have ambiguities (for example, put "-" between each thing you hash, so that a name of "1" plus a count of "23" is not confused with a name of "12" and a count of "3").

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