/
crypto.browser.ts
40 lines (36 loc) · 1.04 KB
/
crypto.browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
export async function generateKey(secret: string, stringToSign: string): Promise<string> {
const key = await window.crypto.subtle.importKey(
"raw",
convertToUint8Array(secret),
{
name: "HMAC",
hash: { name: "SHA-256" }
},
false,
["sign"]
);
const signature = await window.crypto.subtle.sign("HMAC", key, convertToUint8Array(stringToSign));
const base64encodedString = encodeByteArray(new Uint8Array(signature));
const result = encodeURIComponent(base64encodedString);
return result;
}
function convertToUint8Array(value: string) {
const arr = new Uint8Array(value.length);
for (let i = 0; i < value.length; i++) {
arr[i] = value.charCodeAt(i);
}
return arr;
}
/**
* Encodes a byte array in base64 format.
* @param value the Uint8Aray to encode
*/
function encodeByteArray(value: Uint8Array): string {
let str = "";
for (let i = 0; i < value.length; i++) {
str += String.fromCharCode(value[i]);
}
return btoa(str);
}