{"version":3,"file":"MsBrowserCrypto.js","sources":["../../src/crypto/MsBrowserCrypto.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Constants } from \"@azure/msal-common\";\nimport { KEY_FORMAT_JWK } from \"../utils/BrowserConstants\";\nimport { BrowserStringUtils } from \"../utils/BrowserStringUtils\";\nimport { ISubtleCrypto } from \"./ISubtleCrypto\";\n\nexport class MsBrowserCrypto implements ISubtleCrypto {\n getRandomValues(dataBuffer: Uint8Array): Uint8Array {\n return window[\"msCrypto\"].getRandomValues(dataBuffer);\n }\n\n async generateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {\n return new Promise((resolve: Function, reject: Function) => {\n const msGenerateKey = window[\"msCrypto\"].subtle.generateKey(algorithm, extractable, keyUsages);\n msGenerateKey.addEventListener(\"complete\", (e: { target: { result: CryptoKeyPair | PromiseLike; }; }) => {\n resolve(e.target.result);\n });\n\n msGenerateKey.addEventListener(\"error\", (error: string) => {\n reject(error);\n });\n });\n }\n\n async exportKey(key: CryptoKey): Promise {\n return new Promise((resolve: Function, reject: Function) => {\n const msExportKey = window[\"msCrypto\"].subtle.exportKey(KEY_FORMAT_JWK, key);\n msExportKey.addEventListener(\"complete\", (e: { target: { result: ArrayBuffer; }; }) => {\n const resultBuffer: ArrayBuffer = e.target.result;\n\n const resultString = BrowserStringUtils.utf8ArrToString(new Uint8Array(resultBuffer))\n .replace(/\\r/g, Constants.EMPTY_STRING)\n .replace(/\\n/g, Constants.EMPTY_STRING)\n .replace(/\\t/g, Constants.EMPTY_STRING)\n .split(\" \").join(Constants.EMPTY_STRING)\n .replace(\"\\u0000\", Constants.EMPTY_STRING);\n\n try {\n resolve(JSON.parse(resultString));\n } catch (e) {\n reject(e);\n }\n });\n\n msExportKey.addEventListener(\"error\", (error: string) => {\n reject(error);\n });\n });\n }\n\n async importKey(keyData: JsonWebKey, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {\n const keyString = BrowserStringUtils.getSortedObjectString(keyData);\n const keyBuffer = BrowserStringUtils.stringToArrayBuffer(keyString);\n\n return new Promise((resolve: Function, reject: Function) => {\n const msImportKey = window[\"msCrypto\"].subtle.importKey(KEY_FORMAT_JWK, keyBuffer, algorithm, extractable, keyUsages);\n msImportKey.addEventListener(\"complete\", (e: { target: { result: CryptoKey | PromiseLike; }; }) => {\n resolve(e.target.result);\n });\n\n msImportKey.addEventListener(\"error\", (error: string) => {\n reject(error);\n });\n });\n }\n\n async sign(algorithm: AlgorithmIdentifier, key: CryptoKey, data: ArrayBuffer): Promise {\n return new Promise((resolve: Function, reject: Function) => {\n const msSign = window[\"msCrypto\"].subtle.sign(algorithm, key, data);\n msSign.addEventListener(\"complete\", (e: { target: { result: ArrayBuffer | PromiseLike; }; }) => {\n resolve(e.target.result);\n });\n\n msSign.addEventListener(\"error\", (error: string) => {\n reject(error);\n });\n });\n }\n \n async digest(algorithm: AlgorithmIdentifier, data: Uint8Array): Promise {\n return new Promise((resolve, reject) => {\n const digestOperation = window[\"msCrypto\"].subtle.digest(algorithm, data.buffer);\n digestOperation.addEventListener(\"complete\", (e: { target: { result: ArrayBuffer | PromiseLike; }; }) => {\n resolve(e.target.result);\n });\n digestOperation.addEventListener(\"error\", (error: string) => {\n reject(error);\n });\n });\n }\n}\n"],"names":[],"mappings":";;;;;;;AAAA;;;;;IAUA;KAoFC;IAnFG,yCAAe,GAAf,UAAgB,UAAsB;QAClC,OAAO,MAAM,CAAC,UAAU,CAAC,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;KACzD;IAEK,qCAAW,GAAjB,UAAkB,SAAgC,EAAE,WAAoB,EAAE,SAAqB;;;gBAC3F,sBAAO,IAAI,OAAO,CAAC,UAAC,OAAiB,EAAE,MAAgB;wBACnD,IAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,SAAS,EAAE,WA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