decode static method

List<int> decode(
  1. String encoded
)

Decodes a Base64url Encoding string value into a sequence of bytes.

Throws FormatException if the encoded string is not valid Base64url Encoding.

Implementation

static List<int> decode(String encoded) {
  // Detect incorrect "base64url" or normal "base64" encoding
  if (encoded.contains('=')) {
    throw const FormatException('Base64url Encoding: padding not allowed');
  }
  if (encoded.contains('+') || encoded.contains('/')) {
    throw const FormatException('Base64url Encoding: + and / not allowed');
  }

  // Add padding, if necessary
  var output = encoded;
  switch (output.length % 4) {
    case 0:
      break;
    case 2:
      output += '==';
      break;
    case 3:
      output += '=';
      break;
    default:
      throw const FormatException('Base64url Encoding: invalid length');
  }

  // Decode
  return base64Url.decode(output); // this may throw FormatException

  /* Alternative implementation
  var output = encoded.replaceAll('-', '+').replaceAll('_', '/');
  (add padding here)
  return base64Decode(output); // this may throw FormatException
  */
}