{"version":3,"file":"GuidGenerator.js","sources":["../../src/crypto/GuidGenerator.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Constants , IGuidGenerator } from \"@azure/msal-common\";\nimport { MathUtils } from \"../utils/MathUtils\";\nimport { BrowserCrypto } from \"./BrowserCrypto\";\nexport class GuidGenerator implements IGuidGenerator {\n\n // browser crypto object used to generate random values\n private cryptoObj: BrowserCrypto;\n\n constructor(cryptoObj: BrowserCrypto) {\n this.cryptoObj = cryptoObj;\n }\n\n /*\n * RFC4122: The version 4 UUID is meant for generating UUIDs from truly-random or\n * pseudo-random numbers.\n * The algorithm is as follows:\n * Set the two most significant bits (bits 6 and 7) of the\n * clock_seq_hi_and_reserved to zero and one, respectively.\n * Set the four most significant bits (bits 12 through 15) of the\n * time_hi_and_version field to the 4-bit version number from\n * Section 4.1.3. Version4\n * Set all the other bits to randomly (or pseudo-randomly) chosen\n * values.\n * UUID = time-low \"-\" time-mid \"-\"time-high-and-version \"-\"clock-seq-reserved and low(2hexOctet)\"-\" node\n * time-low = 4hexOctet\n * time-mid = 2hexOctet\n * time-high-and-version = 2hexOctet\n * clock-seq-and-reserved = hexOctet:\n * clock-seq-low = hexOctet\n * node = 6hexOctet\n * Format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\n * y could be 1000, 1001, 1010, 1011 since most significant two bits needs to be 10\n * y values are 8, 9, A, B\n */\n generateGuid(): string {\n try {\n const buffer: Uint8Array = new Uint8Array(16);\n this.cryptoObj.getRandomValues(buffer);\n\n // buffer[6] and buffer[7] represents the time_hi_and_version field. We will set the four most significant bits (4 through 7) of buffer[6] to represent decimal number 4 (UUID version number).\n buffer[6] |= 0x40; // buffer[6] | 01000000 will set the 6 bit to 1.\n buffer[6] &= 0x4f; // buffer[6] & 01001111 will set the 4, 5, and 7 bit to 0 such that bits 4-7 == 0100 = \"4\".\n\n // buffer[8] represents the clock_seq_hi_and_reserved field. We will set the two most significant bits (6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively.\n buffer[8] |= 0x80; // buffer[8] | 10000000 will set the 7 bit to 1.\n buffer[8] &= 0xbf; // buffer[8] & 10111111 will set the 6 bit to 0.\n\n return MathUtils.decimalToHex(buffer[0]) + MathUtils.decimalToHex(buffer[1])\n + MathUtils.decimalToHex(buffer[2]) + MathUtils.decimalToHex(buffer[3])\n + \"-\" + MathUtils.decimalToHex(buffer[4]) + MathUtils.decimalToHex(buffer[5])\n + \"-\" + MathUtils.decimalToHex(buffer[6]) + MathUtils.decimalToHex(buffer[7])\n + \"-\" + MathUtils.decimalToHex(buffer[8]) + MathUtils.decimalToHex(buffer[9])\n + \"-\" + MathUtils.decimalToHex(buffer[10]) + MathUtils.decimalToHex(buffer[11])\n + MathUtils.decimalToHex(buffer[12]) + MathUtils.decimalToHex(buffer[13])\n + MathUtils.decimalToHex(buffer[14]) + MathUtils.decimalToHex(buffer[15]);\n }\n catch (err) {\n const guidHolder: string = \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\";\n const hex: string = \"0123456789abcdef\";\n let r: number = 0;\n let guidResponse: string = Constants.EMPTY_STRING;\n for (let i: number = 0; i < 36; i++) {\n if (guidHolder[i] !== \"-\" && guidHolder[i] !== \"4\") {\n // each x and y needs to be random\n r = Math.random() * 16 | 0;\n }\n if (guidHolder[i] === \"x\") {\n guidResponse += hex[r];\n } else if (guidHolder[i] === \"y\") {\n // clock-seq-and-reserved first hex is filtered and remaining hex values are random\n r &= 0x3; // bit and with 0011 to set pos 2 to zero ?0??\n r |= 0x8; // set pos 3 to 1 as 1???\n guidResponse += hex[r];\n } else {\n guidResponse += guidHolder[i];\n }\n }\n return guidResponse;\n }\n }\n\n /**\n * verifies if a string is GUID\n * @param guid\n */\n isGuid(guid: string): boolean {\n const regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;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