MediaWiki:Gadget-libPolyfill-crypto.randomUUID.js:修订间差异
外观
AnnAngela-dbot(留言 | 贡献) 代码变动:103d1a56 - feat: rename (#594) by U:AnnAngela, co-authored-by: GH:github-actions[bot] 标签:由机器人或全自动脚本执行的操作 |
AnnAngela-dbot(留言 | 贡献) 代码变动:103d1a56 - feat: rename (#594) by U:AnnAngela, co-authored-by: GH:github-actions[bot] 标签:由机器人或全自动脚本执行的操作 |
||
| 第9行: | 第9行: | ||
"use strict"; | "use strict"; | ||
(() => { | |||
const REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i; | |||
const validate = (uuid) => "string" === typeof uuid && REGEX.test(uuid); | |||
const rnds8 = new Uint8Array(16); | |||
const rng = () => crypto.getRandomValues(rnds8); | |||
const byteToHex = []; | |||
for (let i = 0; i < 256; ++i) { | |||
for( | |||
byteToHex.push((i + 256).toString(16).substring(1)); | byteToHex.push((i + 256).toString(16).substring(1)); | ||
} | } | ||
const unsafeStringify = (arr, offset = 0) => `${byteToHex[arr[offset + 0]]}${byteToHex[arr[offset + 1]]}${byteToHex[arr[offset + 2]]}${byteToHex[arr[offset + 3]]}-${byteToHex[arr[offset + 4]]}${byteToHex[arr[offset + 5]]}-${byteToHex[arr[offset + 6]]}${byteToHex[arr[offset + 7]]}-${byteToHex[arr[offset + 8]]}${byteToHex[arr[offset + 9]]}-${byteToHex[arr[offset + 10]]}${byteToHex[arr[offset + 11]]}${byteToHex[arr[offset + 12]]}${byteToHex[arr[offset + 13]]}${byteToHex[arr[offset + 14]]}${byteToHex[arr[offset + 15]]}`.toLowerCase(); | |||
const v4 = (options = {}, buf, offset = 0) => { | |||
const rnds = options.random || (options.rng || rng)(); | |||
rnds[6] = rnds[6] & 0x0f | 0x40; | rnds[6] = rnds[6] & 0x0f | 0x40; | ||
rnds[8] = rnds[8] & 0x3f | 0x80; | rnds[8] = rnds[8] & 0x3f | 0x80; | ||
if (buf) { | if (buf) { | ||
for( | for (let i = 0; i < 16; ++i) { | ||
buf[offset + i] = rnds[i]; | buf[offset + i] = rnds[i]; | ||
} | } | ||
| 第59行: | 第31行: | ||
return unsafeStringify(rnds); | return unsafeStringify(rnds); | ||
}; | }; | ||
try { | |||
if (!validate(crypto.randomUUID())) { | if (!validate(crypto.randomUUID())) { | ||
throw void 0; | throw void 0; | ||
} | } | ||
} | } | ||
catch { | |||
crypto.randomUUID = () => v4(); | |||
} | |||
})(); | })(); | ||
/* </pre> */ | /* </pre> */ | ||
2025年7月24日 (四) 20:57的版本
/**
* -------------------------------------------------------------------------
* !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
* -------------------------------------------------------------------------
*/
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/libPolyfill/Gadget-libPolyfill-crypto.randomUUID.js|user=[[U:AnnAngela]]|co-authors=GH:github-actions[bot]|longId=103d1a563ea4ccc8ff29fb55c9bcd88329a56eb5|shortId=103d1a56|summary=feat: rename (#594)|body=<nowiki>Co-authored-by: github-actions[bot] <41898282+github-actions[bot]📧users.noreply.github.com></nowiki>}}';
/* <pre> */
"use strict";
(() => {
const REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
const validate = (uuid) => "string" === typeof uuid && REGEX.test(uuid);
const rnds8 = new Uint8Array(16);
const rng = () => crypto.getRandomValues(rnds8);
const byteToHex = [];
for (let i = 0; i < 256; ++i) {
byteToHex.push((i + 256).toString(16).substring(1));
}
const unsafeStringify = (arr, offset = 0) => `${byteToHex[arr[offset + 0]]}${byteToHex[arr[offset + 1]]}${byteToHex[arr[offset + 2]]}${byteToHex[arr[offset + 3]]}-${byteToHex[arr[offset + 4]]}${byteToHex[arr[offset + 5]]}-${byteToHex[arr[offset + 6]]}${byteToHex[arr[offset + 7]]}-${byteToHex[arr[offset + 8]]}${byteToHex[arr[offset + 9]]}-${byteToHex[arr[offset + 10]]}${byteToHex[arr[offset + 11]]}${byteToHex[arr[offset + 12]]}${byteToHex[arr[offset + 13]]}${byteToHex[arr[offset + 14]]}${byteToHex[arr[offset + 15]]}`.toLowerCase();
const v4 = (options = {}, buf, offset = 0) => {
const rnds = options.random || (options.rng || rng)();
rnds[6] = rnds[6] & 0x0f | 0x40;
rnds[8] = rnds[8] & 0x3f | 0x80;
if (buf) {
for (let i = 0; i < 16; ++i) {
buf[offset + i] = rnds[i];
}
return buf;
}
return unsafeStringify(rnds);
};
try {
if (!validate(crypto.randomUUID())) {
throw void 0;
}
}
catch {
crypto.randomUUID = () => v4();
}
})();
/* </pre> */