const base64decode = (data: string) => {
  const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';

  const string = data + '=='.slice(2 - (data.length & 3));

  let bitmap;
  let result = '';
  let r1;
  let r2;
  let i = 0;

  for (; i < string.length; ) {
    bitmap =
      (b64.indexOf(string.charAt(i++)) << 18) |
      (b64.indexOf(string.charAt(i++)) << 12) |
      ((r1 = b64.indexOf(string.charAt(i++))) << 6) |
      (r2 = b64.indexOf(string.charAt(i++)));

    result +=
      r1 === 64
        ? String.fromCharCode((bitmap >> 16) & 255)
        : r2 === 64
          ? String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255)
          : String.fromCharCode((bitmap >> 16) & 255, (bitmap >> 8) & 255, bitmap & 255);
  }

  return result;
};

export const base64ToHex = (textData: string) => {
  textData = base64decode(textData);

  const data = new Uint8Array(textData.length);

  for (let i = 0; i < textData.length; i++) {
    data[i] = textData.charCodeAt(i);
  }

  return Array.from(data)
    .map((i) => {
      return ('0' + i.toString(16)).slice(-2);
    })
    .join('');
};
