The other way

This commit is contained in:
Manav Rathi 2024-06-05 19:43:39 +05:30
parent b49e829cc5
commit 485614166d
No known key found for this signature in database

View File

@ -398,16 +398,35 @@ export async function toB64(input: Uint8Array) {
return sodium.to_base64(input, sodium.base64_variants.ORIGINAL);
}
/** Convert a {@link Uint8Array} to a URL safe Base64 encoded string. */
export const toB64URLSafe = async (input: Uint8Array) => {
await sodium.ready;
return sodium.to_base64(input, sodium.base64_variants.URLSAFE);
};
/**
* Convert a {@link Uint8Array} to a URL safe Base64 encoded string.
*
* This differs from {@link toB64URLSafe} in that it does not append any
* trailing padding character(s) "=" to make the resultant string's length be an
* integer multiple of 4.
*/
export const toB64URLSafeNoPadding = async (input: Uint8Array) => {
await sodium.ready;
return sodium.to_base64(input, sodium.base64_variants.URLSAFE_NO_PADDING);
};
/**
* Convert a Base64 encoded string to a {@link Uint8Array}.
*
* This is the converse of {@link toB64URLSafeNoPadding}, and does not expect
* its input string's length to be a an integer multiple of 4.
*/
export const fromB64URLSafeNoPadding = async (input: string) => {
await sodium.ready;
return sodium.from_base64(input, sodium.base64_variants.URLSAFE_NO_PADDING);
};
export async function fromUTF8(input: string) {
await sodium.ready;
return sodium.from_string(input);