code: def base64_encode(data):
      
      total = len(data)
      result = []
      mod = 0
      for i in range(total):
          cur = ord(data[i])
          mod = i % 3
          if mod == 0:
              result.append(__enc64__[cur >> 2])
          elif mod == 1:
              prev = ord(data[i - 1])
              result.append(__enc64__[((prev & 3) << 4) | (cur >> 4)])
          elif mod == 2:
              prev = ord(data[i - 1])
              result.append(__enc64__[(prev & 0x0f) << 2 | cur >> 6])
              result.append(__enc64__[cur & 0x3f])
      if mod == 0:
          result.append(__enc64__[(cur & 3) << 4])
          result.append(__enc64__[64] * 2)
      elif mod == 1:
          result.append(__enc64__[(cur & 0x0f) << 2])
          result.append(__enc64__[64])
      return "".join(result)
docs: Base 64 encoder