feat: uuid sem node:crypto (Web Crypto API + MD5 puro), DTS restaurado, bump v0.355.0

This commit is contained in:
marcio 2026-08-13 16:32:17 -03:00
parent fdfe409088
commit 7f9f022b9c
5 changed files with 921 additions and 63 deletions

View file

@ -25,33 +25,143 @@ __export(uuid_exports, {
validarUuid: () => validarUuid
});
module.exports = __toCommonJS(uuid_exports);
var import_node_crypto = require("node:crypto");
const erUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const validarUuid = (uuid2) => {
const retorno = erUuid.test(String(uuid2 || ""));
return retorno;
return erUuid.test(String(uuid2 || ""));
};
const uuidV4 = () => globalThis.crypto.randomUUID();
const uuid = uuidV4;
const NIL_UUID = "00000000-0000-0000-0000-000000000000";
const rotL = (x, n) => x << n | x >>> 32 - n;
const md5 = (str) => {
const msg = new TextEncoder().encode(str);
const len = msg.length;
const bits = len * 8;
const padLen = len % 64 < 56 ? 56 - len % 64 : 120 - len % 64;
const padded = new Uint8Array(len + padLen + 8);
padded.set(msg);
padded[len] = 128;
const dv = new DataView(padded.buffer);
dv.setUint32(len + padLen, bits >>> 0, true);
dv.setUint32(len + padLen + 4, Math.floor(bits / 2 ** 32), true);
const T = Array.from(
{ length: 64 },
(_, i) => Math.abs(Math.sin(i + 1)) * 2 ** 32 >>> 0
);
const S = [
7,
12,
17,
22,
7,
12,
17,
22,
7,
12,
17,
22,
7,
12,
17,
22,
5,
9,
14,
20,
5,
9,
14,
20,
5,
9,
14,
20,
5,
9,
14,
20,
4,
11,
16,
23,
4,
11,
16,
23,
4,
11,
16,
23,
4,
11,
16,
23,
6,
10,
15,
21,
6,
10,
15,
21,
6,
10,
15,
21,
6,
10,
15,
21
];
let a0 = 1732584193, b0 = 4023233417, c0 = 2562383102, d0 = 271733878;
for (let off = 0; off < padded.length; off += 64) {
const M = [];
for (let j = 0; j < 16; j++) M[j] = dv.getUint32(off + j * 4, true);
let [A, B, C, D] = [a0, b0, c0, d0];
for (let i = 0; i < 64; i++) {
let F, g;
if (i < 16) {
F = B & C | ~B & D;
g = i;
} else if (i < 32) {
F = D & B | ~D & C;
g = (5 * i + 1) % 16;
} else if (i < 48) {
F = B ^ C ^ D;
g = (3 * i + 5) % 16;
} else {
F = C ^ (B | ~D);
g = 7 * i % 16;
}
F = F + A + T[i] + M[g] >>> 0;
A = D;
D = C;
C = B;
B = B + rotL(F, S[i]) >>> 0;
}
a0 = a0 + A >>> 0;
b0 = b0 + B >>> 0;
c0 = c0 + C >>> 0;
d0 = d0 + D >>> 0;
}
const toHexLE = (n) => {
let s = "";
for (let i = 0; i < 4; i++) s += (n >>> i * 8 & 255).toString(16).padStart(2, "0");
return s;
};
return toHexLE(a0) + toHexLE(b0) + toHexLE(c0) + toHexLE(d0);
};
const formatarUuidV3 = (hash) => {
const chars = hash.slice(0, 32);
return [
chars.slice(0, 8),
chars.slice(8, 12),
`3${chars.slice(13, 16)}`,
// version 3
`${(Number.parseInt(chars[16], 16) & 3 | 8).toString(16)}${chars.slice(17, 20)}`,
// variant
chars.slice(20, 32)
].join("-");
const c = hash.slice(0, 32);
const v = (Number.parseInt(c[16], 16) & 3 | 8).toString(16);
return `${c.slice(0, 8)}-${c.slice(8, 12)}-3${c.slice(13, 16)}-${v}${c.slice(17, 20)}-${c.slice(20, 32)}`;
};
const uuidV3 = (chave, grupo) => {
const namespace = grupo ? uuidV3(grupo) : NIL_UUID;
const input = typeof chave === "string" ? chave : typeof chave === "number" ? String(chave) : JSON.stringify(chave);
const hash = (0, import_node_crypto.createHash)("md5").update(namespace + input).digest("hex");
return formatarUuidV3(hash);
return formatarUuidV3(md5(namespace + input));
};
const uuidV4 = () => (0, import_node_crypto.randomUUID)();
const uuid = uuidV4;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
erUuid,