forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cosmos] Support browser crypto in Internet Explorer (Azure#8769)
* [Cosmos] Support browser crypto in Internet Explorer * Casing * Update sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts Co-authored-by: Zachary Foster <zfoster@users.noreply.github.com> Co-authored-by: Zachary Foster <zfoster@users.noreply.github.com>
- Loading branch information
1 parent
a98996d
commit 1862965
Showing
5 changed files
with
25 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const globalRef: any = typeof self === "undefined" ? window : self; | ||
|
||
if (!globalThis) { | ||
throw new Error("Could not find global"); | ||
} | ||
|
||
const globalCrypto: Crypto = globalRef.crypto || globalRef.msCrypto; | ||
|
||
if (!globalCrypto || !globalCrypto.subtle) { | ||
throw new Error("Browser does not support cryptography functions"); | ||
} | ||
|
||
export { globalCrypto }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,17 @@ | ||
import { encodeUTF8, encodeBase64 } from "./encode"; | ||
import atob from "./atob"; | ||
|
||
const globalThis = typeof self === "undefined" ? window : self; | ||
import { globalCrypto } from "./globalCrypto"; | ||
|
||
export async function hmac(key: string, message: string) { | ||
if (!globalThis || !globalThis.crypto || !globalThis.crypto.subtle) { | ||
throw new Error("Browser does not support cryptography functions"); | ||
} | ||
const importParams: HmacImportParams = { name: "HMAC", hash: { name: "SHA-256" } }; | ||
const encodedMessage = new Uint8Array( | ||
[...unescape(encodeURIComponent(message))].map((c) => c.charCodeAt(0)) | ||
); | ||
const encodedKey = encodeUTF8(atob(key)); | ||
const cryptoKey = await globalThis.crypto.subtle.importKey( | ||
"raw", | ||
encodedKey, | ||
importParams, | ||
false, | ||
["sign"] | ||
); | ||
const signature = await globalThis.crypto.subtle.sign(importParams, cryptoKey, encodedMessage); | ||
const cryptoKey = await globalCrypto.subtle.importKey("raw", encodedKey, importParams, false, [ | ||
"sign" | ||
]); | ||
const signature = await globalCrypto.subtle.sign(importParams, cryptoKey, encodedMessage); | ||
|
||
return encodeBase64(signature); | ||
} |