|
| 1 | +var modules = (function (exports) { |
| 2 | + 'use strict'; |
| 3 | + |
| 4 | + /*! ***************************************************************************** |
| 5 | + Copyright (c) Microsoft Corporation. |
| 6 | +
|
| 7 | + Permission to use, copy, modify, and/or distribute this software for any |
| 8 | + purpose with or without fee is hereby granted. |
| 9 | +
|
| 10 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 11 | + REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 12 | + AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 13 | + INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 14 | + LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 15 | + OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 16 | + PERFORMANCE OF THIS SOFTWARE. |
| 17 | + ***************************************************************************** */ |
| 18 | + |
| 19 | + function __awaiter(thisArg, _arguments, P, generator) { |
| 20 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 21 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 22 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 23 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 24 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 25 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + const vectorSize = 16; |
| 30 | + const utf8Encoder = new TextEncoder(); |
| 31 | + const utf8Decoder = new TextDecoder(); |
| 32 | + const iterations = 1000; |
| 33 | + const salt = utf8Encoder.encode('XHWnDAT6ehMVY2zD'); |
| 34 | + class CryptoHelperV2 { |
| 35 | + deriveKey(password) { |
| 36 | + return __awaiter(this, void 0, void 0, function* () { |
| 37 | + const buffer = utf8Encoder.encode(password); |
| 38 | + const key = yield crypto.subtle.importKey('raw', buffer, { name: 'PBKDF2' }, false, ['deriveKey']); |
| 39 | + const privateKey = crypto.subtle.deriveKey({ |
| 40 | + name: 'PBKDF2', |
| 41 | + hash: { name: 'SHA-256' }, |
| 42 | + iterations, |
| 43 | + salt |
| 44 | + }, key, { |
| 45 | + name: 'AES-GCM', |
| 46 | + length: 256 |
| 47 | + }, false, ['encrypt', 'decrypt']); |
| 48 | + return privateKey; |
| 49 | + }); |
| 50 | + } |
| 51 | + encryptToBytes(text, password) { |
| 52 | + return __awaiter(this, void 0, void 0, function* () { |
| 53 | + const key = yield this.deriveKey(password); |
| 54 | + const textBytesToEncrypt = utf8Encoder.encode(text); |
| 55 | + const vector = crypto.getRandomValues(new Uint8Array(vectorSize)); |
| 56 | + // encrypt into bytes |
| 57 | + const encryptedBytes = new Uint8Array(yield crypto.subtle.encrypt({ name: 'AES-GCM', iv: vector }, key, textBytesToEncrypt)); |
| 58 | + const finalBytes = new Uint8Array(vector.byteLength + encryptedBytes.byteLength); |
| 59 | + finalBytes.set(vector, 0); |
| 60 | + finalBytes.set(encryptedBytes, vector.byteLength); |
| 61 | + return finalBytes; |
| 62 | + }); |
| 63 | + } |
| 64 | + encryptToBase64(text, password) { |
| 65 | + return __awaiter(this, void 0, void 0, function* () { |
| 66 | + // const key = await this.deriveKey(password); |
| 67 | + // const textBytesToEncrypt = utf8Encoder.encode(text); |
| 68 | + // const vector = crypto.getRandomValues(new Uint8Array(vectorSize)); |
| 69 | + // // encrypt into bytes |
| 70 | + // const encryptedBytes = new Uint8Array( |
| 71 | + // await crypto.subtle.encrypt( |
| 72 | + // {name: 'AES-GCM', iv: vector}, |
| 73 | + // key, |
| 74 | + // textBytesToEncrypt |
| 75 | + // ) |
| 76 | + // ); |
| 77 | + // const finalBytes = new Uint8Array( vector.byteLength + encryptedBytes.byteLength ); |
| 78 | + // finalBytes.set( vector, 0 ); |
| 79 | + // finalBytes.set( encryptedBytes, vector.byteLength ); |
| 80 | + const finalBytes = yield this.encryptToBytes(text, password); |
| 81 | + //convert array to base64 |
| 82 | + const base64Text = btoa(String.fromCharCode(...finalBytes)); |
| 83 | + return base64Text; |
| 84 | + }); |
| 85 | + } |
| 86 | + stringToArray(str) { |
| 87 | + var result = []; |
| 88 | + for (var i = 0; i < str.length; i++) { |
| 89 | + result.push(str.charCodeAt(i)); |
| 90 | + } |
| 91 | + return new Uint8Array(result); |
| 92 | + } |
| 93 | + decryptFromBytes(encryptedBytes, password) { |
| 94 | + return __awaiter(this, void 0, void 0, function* () { |
| 95 | + try { |
| 96 | + // extract iv |
| 97 | + const vector = encryptedBytes.slice(0, vectorSize); |
| 98 | + // extract encrypted text |
| 99 | + const encryptedTextBytes = encryptedBytes.slice(vectorSize); |
| 100 | + const key = yield this.deriveKey(password); |
| 101 | + // decrypt into bytes |
| 102 | + let decryptedBytes = yield crypto.subtle.decrypt({ name: 'AES-GCM', iv: vector }, key, encryptedTextBytes); |
| 103 | + // convert bytes to text |
| 104 | + let decryptedText = utf8Decoder.decode(decryptedBytes); |
| 105 | + return decryptedText; |
| 106 | + } |
| 107 | + catch (e) { |
| 108 | + //console.error(e); |
| 109 | + return null; |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + decryptFromBase64(base64Encoded, password) { |
| 114 | + return __awaiter(this, void 0, void 0, function* () { |
| 115 | + try { |
| 116 | + let bytesToDecode = this.stringToArray(atob(base64Encoded)); |
| 117 | + return yield this.decryptFromBytes(bytesToDecode, password); |
| 118 | + // // extract iv |
| 119 | + // const vector = bytesToDecode.slice(0,vectorSize); |
| 120 | + // // extract encrypted text |
| 121 | + // const encryptedTextBytes = bytesToDecode.slice(vectorSize); |
| 122 | + // const key = await this.deriveKey(password); |
| 123 | + // // decrypt into bytes |
| 124 | + // let decryptedBytes = await crypto.subtle.decrypt( |
| 125 | + // {name: 'AES-GCM', iv: vector}, |
| 126 | + // key, |
| 127 | + // encryptedTextBytes |
| 128 | + // ); |
| 129 | + // // convert bytes to text |
| 130 | + // let decryptedText = utf8Decoder.decode(decryptedBytes); |
| 131 | + // return decryptedText; |
| 132 | + } |
| 133 | + catch (e) { |
| 134 | + //console.error(e); |
| 135 | + return null; |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + const algorithmObsolete = { |
| 141 | + name: 'AES-GCM', |
| 142 | + iv: new Uint8Array([196, 190, 240, 190, 188, 78, 41, 132, 15, 220, 84, 211]), |
| 143 | + tagLength: 128 |
| 144 | + }; |
| 145 | + class CryptoHelperObsolete { |
| 146 | + buildKey(password) { |
| 147 | + return __awaiter(this, void 0, void 0, function* () { |
| 148 | + let utf8Encode = new TextEncoder(); |
| 149 | + let passwordBytes = utf8Encode.encode(password); |
| 150 | + let passwordDigest = yield crypto.subtle.digest({ name: 'SHA-256' }, passwordBytes); |
| 151 | + let key = yield crypto.subtle.importKey('raw', passwordDigest, algorithmObsolete, false, ['encrypt', 'decrypt']); |
| 152 | + return key; |
| 153 | + }); |
| 154 | + } |
| 155 | + encryptToBase64(text, password) { |
| 156 | + return __awaiter(this, void 0, void 0, function* () { |
| 157 | + let key = yield this.buildKey(password); |
| 158 | + let utf8Encode = new TextEncoder(); |
| 159 | + let bytesToEncrypt = utf8Encode.encode(text); |
| 160 | + // encrypt into bytes |
| 161 | + let encryptedBytes = new Uint8Array(yield crypto.subtle.encrypt(algorithmObsolete, key, bytesToEncrypt)); |
| 162 | + //convert array to base64 |
| 163 | + let base64Text = btoa(String.fromCharCode(...encryptedBytes)); |
| 164 | + return base64Text; |
| 165 | + }); |
| 166 | + } |
| 167 | + stringToArray(str) { |
| 168 | + var result = []; |
| 169 | + for (var i = 0; i < str.length; i++) { |
| 170 | + result.push(str.charCodeAt(i)); |
| 171 | + } |
| 172 | + return new Uint8Array(result); |
| 173 | + } |
| 174 | + decryptFromBase64(base64Encoded, password) { |
| 175 | + return __awaiter(this, void 0, void 0, function* () { |
| 176 | + try { |
| 177 | + // convert base 64 to array |
| 178 | + let bytesToDecrypt = this.stringToArray(atob(base64Encoded)); |
| 179 | + let key = yield this.buildKey(password); |
| 180 | + // decrypt into bytes |
| 181 | + let decryptedBytes = yield crypto.subtle.decrypt(algorithmObsolete, key, bytesToDecrypt); |
| 182 | + // convert bytes to text |
| 183 | + let utf8Decode = new TextDecoder(); |
| 184 | + let decryptedText = utf8Decode.decode(decryptedBytes); |
| 185 | + return decryptedText; |
| 186 | + } |
| 187 | + catch (e) { |
| 188 | + return null; |
| 189 | + } |
| 190 | + }); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + exports.CryptoHelperObsolete = CryptoHelperObsolete; |
| 195 | + exports.CryptoHelperV2 = CryptoHelperV2; |
| 196 | + |
| 197 | + Object.defineProperty(exports, '__esModule', { value: true }); |
| 198 | + |
| 199 | + return exports; |
| 200 | + |
| 201 | +})({}); |
0 commit comments