File
Blob: src/lib/encoding.ts
| 1 | export function base64UrlEncode(bytes: Uint8Array): string { |
| 2 | let binary = ""; |
| 3 | for (let i = 0; i < bytes.length; i++) { |
| 4 | binary += String.fromCharCode(bytes[i]); |
| 5 | } |
| 6 | return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); |
| 7 | } |
| 8 | |
| 9 | export function base64UrlDecode(input: string): Uint8Array { |
| 10 | const padded = input.replace(/-/g, "+").replace(/_/g, "/") + "==".slice(0, (4 - (input.length % 4)) % 4); |
| 11 | const binary = atob(padded); |
| 12 | const bytes = new Uint8Array(binary.length); |
| 13 | for (let i = 0; i < binary.length; i++) { |
| 14 | bytes[i] = binary.charCodeAt(i); |
| 15 | } |
| 16 | return bytes; |
| 17 | } |