-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.ts
31 lines (27 loc) · 927 Bytes
/
Utils.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
/**
* Utility function for transforming an ArrayBuffer into a url-safe base64 string without
* padding, used across the WebAuthn API.
* @param arrayBuffer
*/
export const arrayBufferToWebauthnB64 = (arrayBuffer: ArrayBuffer) => {
return btoa(String.fromCharCode(
...new Uint8Array(arrayBuffer)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=/g, "");
}
/**
* Utility function for transforming a url-safe base64 string without padding
* into an ArrayBuffer.
*/
export const webauthnB64ToArrayBuffer = (b64String: String) => {
let fromUrlSafe = b64String
.replace(/\_/g, "/").replace(/\-/g, "+")
while (fromUrlSafe.length % 4 !== 0) {
fromUrlSafe += "=";
}
const decoded = atob(fromUrlSafe);
// @ts-ignore
const arrayBuffer = Uint8Array.from(decoded, c => c.charCodeAt(0)).buffer;
return arrayBuffer as ArrayBuffer;
}