2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2021-07-20 22:27:41 +00:00
|
|
|
import base64js from 'base64-js';
|
|
|
|
import { TextDecoderLite, TextEncoderLite } from 'text-encoder-lite';
|
|
|
|
|
|
|
|
export { base64EncodeString, base64DecodeString };
|
|
|
|
|
|
|
|
function base64EncodeString(string) {
|
|
|
|
if (!string) {
|
|
|
|
string = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoded = new TextEncoderLite('utf-8').encode(string);
|
|
|
|
return base64js.fromByteArray(encoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
function base64DecodeString(b64String) {
|
|
|
|
if (!b64String) {
|
|
|
|
b64String = base64EncodeString('');
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8array = base64js.toByteArray(b64String);
|
|
|
|
return new TextDecoderLite('utf-8').decode(uint8array);
|
|
|
|
}
|