UNPKG

@cocalc/backend

Version:

CoCalc backend functionality: functionality used by either the hub, the next.js server or the project.

39 lines 1.34 kB
"use strict"; /* sha1 hash functionality */ Object.defineProperty(exports, "__esModule", { value: true }); exports.uuidsha1 = exports.sha1 = void 0; const crypto_1 = require("crypto"); // compute sha1 hash of data in hex function sha1(data) { if (typeof data === "string") { // CRITICAL: Code below assumes data is a Buffer; it will seem to work on a string, but give // the wrong result where wrong means that it doesn't agree with the frontend version defined // in misc. data = Buffer.from(data); } const sha1sum = (0, crypto_1.createHash)("sha1"); sha1sum.update(data); return sha1sum.digest("hex"); } exports.sha1 = sha1; // Compute a uuid v4 from the Sha-1 hash of data. // Optionally, if knownSha1 is given, just uses that, rather than recomputing it. function uuidsha1(data, knownSha1) { const s = knownSha1 ?? sha1(data); let i = -1; return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { i += 1; switch (c) { case "x": return s[i] ?? "0"; case "y": // take 8 + low order 3 bits of hex number. return ((parseInt("0x" + s[i], 16) & 0x3) | 0x8).toString(16); } return "0"; }); } exports.uuidsha1 = uuidsha1; //# sourceMappingURL=sha1.js.map