Javascript 中的 UUIDv6、v7、v8(浏览器)

发布于 2025-01-20 07:46:27 字数 818 浏览 1 评论 0原文

我一直在尝试找到用于在浏览器中生成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 技术交流群。

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

发布评论

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

评论(2

滿滿的愛 2025-01-27 07:46:27

试试这个片段:

const v4 = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    const r = Math.trunc(Math.random() * 16);
    const v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

const v7 = () => {
  return 'tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    const r = Math.trunc(Math.random() * 16);
    const v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  }).replace(/^[t]{8}-[t]{4}/, function() {
    const unixtimestamp = Date.now().toString(16).padStart(12, '0');
    return unixtimestamp.slice(0, 8) + '-' + unixtimestamp.slice(8);
  });
}

console.log(v4());
console.log(v7());

// Also see: https://gist.github.com/fabiolimace/c725349dd34aedc7b69867dabec59c08

要点: https://gist.github.com/fabiolimace/c0c11c5ea013d4ec54cf6b0d43d366c6

Try this snippet:

const v4 = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    const r = Math.trunc(Math.random() * 16);
    const v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

const v7 = () => {
  return 'tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    const r = Math.trunc(Math.random() * 16);
    const v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  }).replace(/^[t]{8}-[t]{4}/, function() {
    const unixtimestamp = Date.now().toString(16).padStart(12, '0');
    return unixtimestamp.slice(0, 8) + '-' + unixtimestamp.slice(8);
  });
}

console.log(v4());
console.log(v7());

// Also see: https://gist.github.com/fabiolimace/c725349dd34aedc7b69867dabec59c08

Gist: https://gist.github.com/fabiolimace/c0c11c5ea013d4ec54cf6b0d43d366c6

轻许诺言 2025-01-27 07:46:27

这不是确定的答案,但是我在阅读 buildkite post 和a 在reddit上。

我修改了 uuidv7 项目可以在普通的JavaScript而不是Typescript中工作。我的修改可能会错过一堆边缘案例,例如seq始终是0,但至少是一开始。我将它留在这里,让别人告诉我我做错了什么。

function uuidv7 () {
    const UNIX_TS_MS_BITS = 48;
    const VER_DIGIT = "7";
    const SEQ_BITS = 12;
    const VAR = 0b10;
    const VAR_BITS = 2;
    const RAND_BITS = 62;
    
    let prevTimestamp = -1;
    let seq = 0;

    const timestamp = Math.max(Date.now(), prevTimestamp);
    seq = timestamp === prevTimestamp ? seq + 1 : 0;
    prevTimestamp = timestamp;
    
    const var_rand = new Uint32Array(2);
    crypto.getRandomValues(var_rand);
    var_rand[0] = (VAR << (32 - VAR_BITS)) | (var_rand[0] >>> VAR_BITS);
    
    const digits =
    timestamp.toString(16).padStart(UNIX_TS_MS_BITS / 4, "0") +
    VER_DIGIT +
    seq.toString(16).padStart(SEQ_BITS / 4, "0") +
    var_rand[0].toString(16).padStart((VAR_BITS + RAND_BITS) / 2 / 4, "0") +
    var_rand[1].toString(16).padStart((VAR_BITS + RAND_BITS) / 2 / 4, "0");
    
    return (
        digits.slice(0, 8) +
        "-" +
        digits.slice(8, 12) +
        "-" +
        digits.slice(12, 16) +
        "-" +
        digits.slice(16, 20) +
        "-" +
        digits.slice(20)
    );
}

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 being 0, but at least it's something to start with. I'll leave it here for others to tell me what I did wrong.

function uuidv7 () {
    const UNIX_TS_MS_BITS = 48;
    const VER_DIGIT = "7";
    const SEQ_BITS = 12;
    const VAR = 0b10;
    const VAR_BITS = 2;
    const RAND_BITS = 62;
    
    let prevTimestamp = -1;
    let seq = 0;

    const timestamp = Math.max(Date.now(), prevTimestamp);
    seq = timestamp === prevTimestamp ? seq + 1 : 0;
    prevTimestamp = timestamp;
    
    const var_rand = new Uint32Array(2);
    crypto.getRandomValues(var_rand);
    var_rand[0] = (VAR << (32 - VAR_BITS)) | (var_rand[0] >>> VAR_BITS);
    
    const digits =
    timestamp.toString(16).padStart(UNIX_TS_MS_BITS / 4, "0") +
    VER_DIGIT +
    seq.toString(16).padStart(SEQ_BITS / 4, "0") +
    var_rand[0].toString(16).padStart((VAR_BITS + RAND_BITS) / 2 / 4, "0") +
    var_rand[1].toString(16).padStart((VAR_BITS + RAND_BITS) / 2 / 4, "0");
    
    return (
        digits.slice(0, 8) +
        "-" +
        digits.slice(8, 12) +
        "-" +
        digits.slice(12, 16) +
        "-" +
        digits.slice(16, 20) +
        "-" +
        digits.slice(20)
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文