UNPKG

945 BPlain TextView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3
4/**
5 * Encodes a string in base64 format.
6 * @param value the string to encode
7 */
8export function encodeString(value: string): string {
9 return btoa(value);
10}
11
12/**
13 * Encodes a byte array in base64 format.
14 * @param value the Uint8Aray to encode
15 */
16export function encodeByteArray(value: Uint8Array): string {
17 let str = "";
18 for (let i = 0; i < value.length; i++) {
19 str += String.fromCharCode(value[i]);
20 }
21 return btoa(str);
22}
23
24/**
25 * Decodes a base64 string into a byte array.
26 * @param value the base64 string to decode
27 */
28export function decodeString(value: string): Uint8Array {
29 const byteString = atob(value);
30 const arr = new Uint8Array(byteString.length);
31 for (let i = 0; i < byteString.length; i++) {
32 arr[i] = byteString.charCodeAt(i);
33 }
34 return arr;
35}