UNPKG

6.98 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License.
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.CryptoUtils = void 0;
8/**
9 * @hidden
10 */
11var CryptoUtils = /** @class */ (function () {
12 function CryptoUtils() {
13 }
14 /**
15 * Creates a new random GUID
16 * @returns string (GUID)
17 */
18 CryptoUtils.createNewGuid = function () {
19 /*
20 * RFC4122: The version 4 UUID is meant for generating UUIDs from truly-random or
21 * pseudo-random numbers.
22 * The algorithm is as follows:
23 * Set the two most significant bits (bits 6 and 7) of the
24 * clock_seq_hi_and_reserved to zero and one, respectively.
25 * Set the four most significant bits (bits 12 through 15) of the
26 * time_hi_and_version field to the 4-bit version number from
27 * Section 4.1.3. Version4
28 * Set all the other bits to randomly (or pseudo-randomly) chosen
29 * values.
30 * UUID = time-low "-" time-mid "-"time-high-and-version "-"clock-seq-reserved and low(2hexOctet)"-" node
31 * time-low = 4hexOctet
32 * time-mid = 2hexOctet
33 * time-high-and-version = 2hexOctet
34 * clock-seq-and-reserved = hexOctet:
35 * clock-seq-low = hexOctet
36 * node = 6hexOctet
37 * Format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
38 * y could be 1000, 1001, 1010, 1011 since most significant two bits needs to be 10
39 * y values are 8, 9, A, B
40 */
41 var cryptoObj = window.crypto; // for IE 11
42 if (cryptoObj && cryptoObj.getRandomValues) {
43 var buffer = new Uint8Array(16);
44 cryptoObj.getRandomValues(buffer);
45 // 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).
46 buffer[6] |= 0x40; // buffer[6] | 01000000 will set the 6 bit to 1.
47 buffer[6] &= 0x4f; // buffer[6] & 01001111 will set the 4, 5, and 7 bit to 0 such that bits 4-7 == 0100 = "4".
48 // 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.
49 buffer[8] |= 0x80; // buffer[8] | 10000000 will set the 7 bit to 1.
50 buffer[8] &= 0xbf; // buffer[8] & 10111111 will set the 6 bit to 0.
51 return CryptoUtils.decimalToHex(buffer[0]) + CryptoUtils.decimalToHex(buffer[1])
52 + CryptoUtils.decimalToHex(buffer[2]) + CryptoUtils.decimalToHex(buffer[3])
53 + "-" + CryptoUtils.decimalToHex(buffer[4]) + CryptoUtils.decimalToHex(buffer[5])
54 + "-" + CryptoUtils.decimalToHex(buffer[6]) + CryptoUtils.decimalToHex(buffer[7])
55 + "-" + CryptoUtils.decimalToHex(buffer[8]) + CryptoUtils.decimalToHex(buffer[9])
56 + "-" + CryptoUtils.decimalToHex(buffer[10]) + CryptoUtils.decimalToHex(buffer[11])
57 + CryptoUtils.decimalToHex(buffer[12]) + CryptoUtils.decimalToHex(buffer[13])
58 + CryptoUtils.decimalToHex(buffer[14]) + CryptoUtils.decimalToHex(buffer[15]);
59 }
60 else {
61 var guidHolder = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
62 var hex = "0123456789abcdef";
63 var r = 0;
64 var guidResponse = "";
65 for (var i = 0; i < 36; i++) {
66 if (guidHolder[i] !== "-" && guidHolder[i] !== "4") {
67 // each x and y needs to be random
68 r = Math.random() * 16 | 0;
69 }
70 if (guidHolder[i] === "x") {
71 guidResponse += hex[r];
72 }
73 else if (guidHolder[i] === "y") {
74 // clock-seq-and-reserved first hex is filtered and remaining hex values are random
75 r &= 0x3; // bit and with 0011 to set pos 2 to zero ?0??
76 r |= 0x8; // set pos 3 to 1 as 1???
77 guidResponse += hex[r];
78 }
79 else {
80 guidResponse += guidHolder[i];
81 }
82 }
83 return guidResponse;
84 }
85 };
86 /**
87 * verifies if a string is GUID
88 * @param guid
89 */
90 CryptoUtils.isGuid = function (guid) {
91 var 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;
92 return regexGuid.test(guid);
93 };
94 /**
95 * Decimal to Hex
96 *
97 * @param num
98 */
99 CryptoUtils.decimalToHex = function (num) {
100 var hex = num.toString(16);
101 while (hex.length < 2) {
102 hex = "0" + hex;
103 }
104 return hex;
105 };
106 // See: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_4_%E2%80%93_escaping_the_string_before_encoding_it
107 /**
108 * encoding string to base64 - platform specific check
109 *
110 * @param input
111 */
112 CryptoUtils.base64Encode = function (input) {
113 return btoa(encodeURIComponent(input).replace(/%([0-9A-F]{2})/g, function toSolidBytes(match, p1) {
114 return String.fromCharCode(Number("0x" + p1));
115 }));
116 };
117 /**
118 * Decodes a base64 encoded string.
119 *
120 * @param input
121 */
122 CryptoUtils.base64Decode = function (input) {
123 var encodedString = input.replace(/-/g, "+").replace(/_/g, "/");
124 switch (encodedString.length % 4) {
125 case 0:
126 break;
127 case 2:
128 encodedString += "==";
129 break;
130 case 3:
131 encodedString += "=";
132 break;
133 default:
134 throw new Error("Invalid base64 string");
135 }
136 return decodeURIComponent(atob(encodedString).split("").map(function (c) {
137 return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
138 }).join(""));
139 };
140 /**
141 * deserialize a string
142 *
143 * @param query
144 */
145 CryptoUtils.deserialize = function (query) {
146 var match; // Regex for replacing addition symbol with a space
147 var pl = /\+/g;
148 var search = /([^&=]+)=([^&]*)/g;
149 var decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); };
150 var obj = {};
151 match = search.exec(query);
152 while (match) {
153 obj[decode(match[1])] = decode(match[2]);
154 match = search.exec(query);
155 }
156 return obj;
157 };
158 return CryptoUtils;
159}());
160exports.CryptoUtils = CryptoUtils;
161//# sourceMappingURL=CryptoUtils.js.map
\No newline at end of file