UUID4 的较短版本

swi*_*tch 2 python uuid

我正在 python uuid4 字符串中生成。

我用它来识别我的服务帐户,但我的系统之一(GCP 服务帐户)有 30 个字符的限制,现在使用其他帐户为时已晚:

Service account ID must be between 6 and 30 characters.
Service account ID must start with a lower case letter, followed by one or more lower case alphanumerical characters that can be separated by hyphens.
Run Code Online (Sandbox Code Playgroud)

如何获得具有有限冲突风险的较短版本的 UUID?

我见过一些 base64 编码 hack,但我能做的最短是 22。理想情况下,我会拥有类似 git commit hash 的东西,因为冲突的风险是有限的。

wim*_*wim 5

如何获得具有有限冲突风险的较短版本的 UUID?

删除索引 12 处的字符,因为它始终是 4 -不是很随机

同样,删除索引 16 处的字符,因为它始终位于 8 9 a b 中。

[0-9a-f] 中将留下 30 个相当随机的字符。碰撞仍然小到可以忽略。请注意,uuid4 可能以数字开头,因此您可能需要在生成器中使用拒绝采样(或者直接删除中间人并直接使用 生成 id random,实际上这里使用 uuid4 没有多大意义)。