Javascript 中的 UUIDv6、v7、v8(浏览器)
我一直在尝试找到用于在浏览器中生成UUID的JavaScript解决方案。 我找到的 遵循版本4标准生成UUID。
示例:
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
console.log(uuidv4());
甚至
let uuid = self.crypto.randomUUID();
console.log(uuid);
其他版本是否在浏览器中不可能生成,因为它们使用MAC地址(由于隐私问题,在JS中无法访问这些地址)?
据我了解, v6 主要是uuIDV1(),因此也无法使用。但是 v7 和 v8 呢?是否有一个方面阻止在线使用(客户端)?还是可以调整V4代码来生产这些代码?
I have been trying to find Javascript solutions for generating UUIDs in the browser. The ones I have found generate UUIDs following the version 4 standard.
Example:
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
console.log(uuidv4());
or even
let uuid = self.crypto.randomUUID();
console.log(uuid);
Are the other versions impossible to generate in the browser because they use MAC addresses (and those are inaccessible in JS due to privacy concerns)?
As I understand it, V6 is mostly UUIDv1 with some reordering (Draft), so it's not possible to use either. But how about V7 and V8? Is there an aspect preventing online (client-side) use? Or could the V4 code be adapted to produce those?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个片段:
要点: https://gist.github.com/fabiolimace/c0c11c5ea013d4ec54cf6b0d43d366c6
Try this snippet:
Gist: https://gist.github.com/fabiolimace/c0c11c5ea013d4ec54cf6b0d43d366c6
这不是确定的答案,但是我在阅读 buildkite post 和a 在reddit上。
我修改了 uuidv7 项目可以在普通的JavaScript而不是Typescript中工作。我的修改可能会错过一堆边缘案例,例如
seq
始终是0
,但至少是一开始。我将它留在这里,让别人告诉我我做错了什么。Not a definitive answer, but I managed to piece together a solution for UUID v7 in the browser, after I read the post by buildkite, and a post on Reddit.
I modified the UUIDv7 project to work in plain Javascript instead of Typescript. My modifications probably miss a bunch of edge cases, like
seq
always being0
, but at least it's something to start with. I'll leave it here for others to tell me what I did wrong.