base32Encode function

String base32Encode(
  1. Uint8List input
)

Implementation

String base32Encode(Uint8List input) {
  // How many bits will we skip from the first byte.
  int skip = 0;
  // 5 high bits, carry from one byte to the next.
  int bits = 0;

  // The output string in base32.
  String output = '';

  int encodeByte(int byte) {
    if (skip < 0) {
      // we have a carry from the previous byte
      bits |= byte >> -skip;
    } else {
      // no carry
      bits = (byte << skip) & 248;
    }

    if (skip > 3) {
      // Not enough data to produce a character, get us another one
      skip -= 8;
      return 1;
    }

    if (skip < 4) {
      // produce a character
      output += _alphabet[bits >> 3];
      skip += 5;
    }

    return 0;
  }

  for (int i = 0; i < input.length;) {
    i += encodeByte(input[i]);
  }

  return output + (skip < 0 ? _alphabet[bits >> 3] : '');
}