Files
2025-07-26 14:15:09 +02:00

21 lines
801 B
TypeScript

export function b64UrlEncode(data: Uint8Array): string {
const base64 = btoa(String.fromCharCode(...data));
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
export function b64UrlDecode(encoded: string): Uint8Array {
// Replace URL-safe characters with standard base64 characters
let base64 = encoded.replace(/-/g, '+').replace(/_/g, '/');
// Add padding if necessary
const padding = base64.length % 4;
if (padding) {
base64 += '='.repeat(4 - padding);
}
// Decode the base64 string to a byte array
const binaryString = atob(base64);
const byteArray = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
byteArray[i] = binaryString.charCodeAt(i);
}
return byteArray;
}