From 1d532ca374f181932a24a83fa98f71a5bd4f3e96 Mon Sep 17 00:00:00 2001 From: doomgirl <166654615+doomgirl@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:44:21 +0000 Subject: [PATCH] fix: seq_hi shift for byte 6 (#775) --- src/v7.js | 2 +- test/unit/v7.test.js | 45 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/v7.js b/src/v7.js index 515cbc7a..b18346ff 100644 --- a/src/v7.js +++ b/src/v7.js @@ -125,7 +125,7 @@ function v7(options, buf, offset) { b[i++] = _msecs & 0xff; // [byte 6] - set 4 bits of version (7) with first 4 bits seq_hi - b[i++] = ((seqHigh >>> 4) & 0x0f) | 0x70; + b[i++] = ((seqHigh >>> 8) & 0x0f) | 0x70; // [byte 7] remaining 8 bits of seq_hi b[i++] = seqHigh & 0xff; diff --git a/test/unit/v7.test.js b/test/unit/v7.test.js index aa2b5430..404b7b00 100644 --- a/test/unit/v7.test.js +++ b/test/unit/v7.test.js @@ -1,5 +1,6 @@ import assert from 'assert'; import v7 from '../../src/v7.js'; +import stringify from '../../src/stringify.js'; /** * fixture bit layout: @@ -154,7 +155,7 @@ describe('v7', () => { seq, }); - assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7fff-bfff-f'); + assert.strictEqual(uuid.substr(0, 25), '017f22e2-79b0-7dff-bfff-f'); }); test('internal seq is reset upon timestamp change', () => { @@ -169,4 +170,46 @@ describe('v7', () => { assert(uuid.indexOf('fff') !== 15); }); + + test('flipping bits changes the result', () => { + // convert uint8array to BigInt (BE) + const asBigInt = (buf) => buf.reduce((acc, v) => (acc << 8n) | BigInt(v), 0n); + + // convert the given number of bits (LE) to number + const asNumber = (bits, data) => Number(BigInt.asUintN(bits, data)); + + // flip the nth bit (BE) in a BigInt + const flip = (data, n) => data ^ (1n << BigInt(127 - n)); + + // Extract v7 `options` from a (BigInt) UUID + const optionsFrom = (data) => { + const ms = asNumber(48, data >> (128n - 48n)); + const hi = asNumber(12, data >> (43n + 19n + 2n)); + const lo = asNumber(19, data >> 43n); + const r = BigInt.asUintN(43, data); + return { + msecs: ms, + seq: (hi << 19) | lo, + random: [ + ...Array(10).fill(0), + ...Array(6) + .fill(0) + .map((_, i) => asNumber(8, r >> (BigInt(i) * 8n))) + .reverse(), + ], + }; + }; + const buf = new Uint8Array(16); + const data = asBigInt(v7({}, buf)); + const id = stringify(buf); + const reserved = [48, 49, 50, 51, 64, 65]; + for (let i = 0; i < 128; ++i) { + if (reserved.includes(i)) { + continue; // skip bits used for version and variant + } + const flipped = flip(data, i); + assert.strictEqual(asBigInt(v7(optionsFrom(flipped), buf)), flipped, i); + assert.notStrictEqual(stringify(buf), id); + } + }); });