UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

250 lines (249 loc) 7.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const RandomUtils_1 = require("./RandomUtils"); function f(s, x, y, z) { switch (s) { case 0: return (x & y) ^ (~x & z); case 1: return x ^ y ^ z; case 2: return (x & y) ^ (x & z) ^ (y & z); case 3: return x ^ y ^ z; } } function ROTL(x, n) { return (x << n) | (x >>> (32 - n)); } function sha1(bytes) { const K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6]; const H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]; if (typeof bytes === 'string') { const msg = decodeURIComponent(encodeURIComponent(bytes)); bytes = new Array(msg.length); for (let i = 0; i < msg.length; i++) { bytes[i] = msg.charCodeAt(i); } } bytes.push(0x80); const l = bytes.length / 4 + 2; const N = Math.ceil(l / 16); const M = new Array(N); for (let i = 0; i < N; i++) { M[i] = new Array(16); for (let j = 0; j < 16; j++) { M[i][j] = (bytes[i * 64 + j * 4] << 24) | (bytes[i * 64 + j * 4 + 1] << 16) | (bytes[i * 64 + j * 4 + 2] << 8) | bytes[i * 64 + j * 4 + 3]; } } M[N - 1][14] = ((bytes.length - 1) * 8) / Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14]); M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff; for (let i = 0; i < N; i++) { const W = new Array(80); for (let t = 0; t < 16; t++) W[t] = M[i][t]; for (let t = 16; t < 80; t++) { W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1); } let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4]; for (let t = 0; t < 80; t++) { const s = Math.floor(t / 20); const T = (ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t]) >>> 0; e = d; d = c; c = ROTL(b, 30) >>> 0; b = a; a = T; } H[0] = (H[0] + a) >>> 0; H[1] = (H[1] + b) >>> 0; H[2] = (H[2] + c) >>> 0; H[3] = (H[3] + d) >>> 0; H[4] = (H[4] + e) >>> 0; } return [ (H[0] >> 24) & 0xff, (H[0] >> 16) & 0xff, (H[0] >> 8) & 0xff, H[0] & 0xff, (H[1] >> 24) & 0xff, (H[1] >> 16) & 0xff, (H[1] >> 8) & 0xff, H[1] & 0xff, (H[2] >> 24) & 0xff, (H[2] >> 16) & 0xff, (H[2] >> 8) & 0xff, H[2] & 0xff, (H[3] >> 24) & 0xff, (H[3] >> 16) & 0xff, (H[3] >> 8) & 0xff, H[3] & 0xff, (H[4] >> 24) & 0xff, (H[4] >> 16) & 0xff, (H[4] >> 8) & 0xff, H[4] & 0xff, ]; } function rng() { return RandomUtils_1.randomBytes(16); } const byteToHex = []; for (let i = 0; i < 256; ++i) { byteToHex[i] = (i + 0x100).toString(16).substr(1); } function bytesToUuid(buf, offset) { let i = offset || 0; const bth = byteToHex; return (bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + '-' + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]] + bth[buf[i++]]); } function v1(options, buf, offset) { const _seedBytes = rng(); const _nodeId = [_seedBytes[0] | 0x01, _seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]]; let _clockseq = ((_seedBytes[6] << 8) | _seedBytes[7]) & 0x3fff; let _lastMSecs = 0, _lastNSecs = 0; let i = (buf && offset) || 0; const b = buf || []; options = options || {}; let clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; let msecs = options.msecs !== undefined ? options.msecs : new Date().getTime(); let nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; const dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; if (dt < 0 && options.clockseq === undefined) { clockseq = (clockseq + 1) & 0x3fff; } if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) { nsecs = 0; } if (nsecs >= 10000) { throw new Error("uuid.v1(): Can't create more than 10M uuids/sec"); } _lastMSecs = msecs; _lastNSecs = nsecs; _clockseq = clockseq; msecs += 12219292800000; const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000; b[i++] = (tl >>> 24) & 0xff; b[i++] = (tl >>> 16) & 0xff; b[i++] = (tl >>> 8) & 0xff; b[i++] = tl & 0xff; const tmh = ((msecs / 0x100000000) * 10000) & 0xfffffff; b[i++] = (tmh >>> 8) & 0xff; b[i++] = tmh & 0xff; b[i++] = ((tmh >>> 24) & 0xf) | 0x10; b[i++] = (tmh >>> 16) & 0xff; b[i++] = (clockseq >>> 8) | 0x80; b[i++] = clockseq & 0xff; const node = options.node || _nodeId; for (let n = 0; n < 6; ++n) { b[i + n] = node[n]; } return buf ? buf : bytesToUuid(b); } function v4(options, buf, offset) { const i = (buf && offset) || 0; if (typeof options === 'string') { buf = options === 'binary' ? new Array(16) : null; options = null; } options = options || {}; const rnds = options.random || (options.rng || rng)(); rnds[6] = (rnds[6] & 0x0f) | 0x40; rnds[8] = (rnds[8] & 0x3f) | 0x80; if (buf) { for (let ii = 0; ii < 16; ++ii) { buf[i + ii] = rnds[ii]; } } return buf || bytesToUuid(rnds); } function uuidToBytes(uuid) { const bytes = []; uuid.replace(/[a-fA-F0-9]{2}/g, (hex) => { bytes.push(parseInt(hex, 16)); }); return bytes; } function stringToBytes(str) { str = decodeURI(encodeURIComponent(str)); const bytes = new Array(str.length); for (let i = 0; i < str.length; i++) { bytes[i] = str.charCodeAt(i); } return bytes; } function v5(name, namespace, buf, offset) { const off = (buf && offset) || 0; if (typeof name === 'string') name = stringToBytes(name); if (typeof namespace === 'string') namespace = uuidToBytes(namespace); if (!Array.isArray(name)) throw TypeError('name must be an array of bytes'); if (!Array.isArray(namespace) || namespace.length !== 16) { throw TypeError('namespace must be uuid string or an Array of 16 byte values'); } const bytes = sha1(namespace.concat(name)); bytes[6] = (bytes[6] & 0x0f) | 0x50; bytes[8] = (bytes[8] & 0x3f) | 0x80; if (buf) { for (let idx = 0; idx < 16; ++idx) { buf[off + idx] = bytes[idx]; } } return buf || bytesToUuid(bytes); } function uuid(options) { let version = 'v4'; if (options) { version = options.version || version; } switch (version) { case 'v1': return v1(); case 'v4': return v4(); case 'v5': return v5(); default: return v4(); } } exports.uuid = uuid; function uuidv1() { return uuid({ version: 'v1' }); } exports.uuidv1 = uuidv1; function uuidv4() { return uuid({ version: 'v4' }); } exports.uuidv4 = uuidv4; function uuidv5() { return uuid({ version: 'v5' }); } exports.uuidv5 = uuidv5;