base64Encode function

String base64Encode(
  1. List<int> bytes
)

Implementation

String base64Encode(List<int> bytes) {
  const String base64Chars =
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

  String result = '';
  int i = 0;

  while (i < bytes.length) {
    int byte1 = bytes[i++];
    int byte2 = i < bytes.length ? bytes[i++] : 0;
    int byte3 = i < bytes.length ? bytes[i++] : 0;

    int triple = (byte1 << 16) + (byte2 << 8) + byte3;

    result += base64Chars[(triple >> 18) & 0x3F];
    result += base64Chars[(triple >> 12) & 0x3F];
    result +=
        (i - 1) > bytes.length - 2 ? '=' : base64Chars[(triple >> 6) & 0x3F];
    result += (i) > bytes.length - 1 ? '=' : base64Chars[triple & 0x3F];
  }

  return result;
}