Path: blob/main/extensions/markdown-language-features/src/util/uuid.ts
4774 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45/**6* Copied from src/vs/base/common/uuid.ts7*/8export function generateUuid(): string {9// use `randomUUID` if possible10if (typeof crypto.randomUUID === 'function') {11// see https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto12// > Although crypto is available on all windows, the returned Crypto object only has one13// > usable feature in insecure contexts: the getRandomValues() method.14// > In general, you should use this API only in secure contexts.1516return crypto.randomUUID.bind(crypto)();17}1819// prep-work20const _data = new Uint8Array(16);21const _hex: string[] = [];22for (let i = 0; i < 256; i++) {23_hex.push(i.toString(16).padStart(2, '0'));24}2526// get data27crypto.getRandomValues(_data);2829// set version bits30_data[6] = (_data[6] & 0x0f) | 0x40;31_data[8] = (_data[8] & 0x3f) | 0x80;3233// print as string34let i = 0;35let result = '';36result += _hex[_data[i++]];37result += _hex[_data[i++]];38result += _hex[_data[i++]];39result += _hex[_data[i++]];40result += '-';41result += _hex[_data[i++]];42result += _hex[_data[i++]];43result += '-';44result += _hex[_data[i++]];45result += _hex[_data[i++]];46result += '-';47result += _hex[_data[i++]];48result += _hex[_data[i++]];49result += '-';50result += _hex[_data[i++]];51result += _hex[_data[i++]];52result += _hex[_data[i++]];53result += _hex[_data[i++]];54result += _hex[_data[i++]];55result += _hex[_data[i++]];56return result;57}585960