Home Reference Source

src/utils/numeric-encoding-utils.ts

  1. export function base64ToBase64Url(base64encodedStr: string): string {
  2. return base64encodedStr
  3. .replace(/\+/g, '-')
  4. .replace(/\//g, '_')
  5. .replace(/=+$/, '');
  6. }
  7.  
  8. export function strToBase64Encode(str: string): string {
  9. return btoa(str);
  10. }
  11.  
  12. export function base64DecodeToStr(str: string): string {
  13. return atob(str);
  14. }
  15.  
  16. export function base64Encode(input: Uint8Array): string {
  17. return btoa(String.fromCharCode(...input));
  18. }
  19.  
  20. export function base64UrlEncode(input: Uint8Array): string {
  21. return base64ToBase64Url(base64Encode(input));
  22. }
  23.  
  24. export function base64Decode(base64encodedStr: string): Uint8Array {
  25. return Uint8Array.from(atob(base64encodedStr), (c) => c.charCodeAt(0));
  26. }