_comuns/dist-back/uuid.js

172 lines
4 KiB
JavaScript

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var uuid_exports = {};
__export(uuid_exports, {
erUuid: () => erUuid,
uuid: () => uuid,
uuidV3: () => uuidV3,
uuidV4: () => uuidV4,
validarUuid: () => validarUuid
});
module.exports = __toCommonJS(uuid_exports);
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) => {
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 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);
return formatarUuidV3(md5(namespace + input));
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
erUuid,
uuid,
uuidV3,
uuidV4,
validarUuid
});