encodeBase64WithCharset static method
Implementation
static String encodeBase64WithCharset(
{required String base64Input, required String charset}) {
if (base64Input.isEmpty) return '';
List<int> bytes = base64.decode(base64Input);
int n = charset.length;
if (n < 2)
throw Exception('Charset must have at least 2 unique characters');
BigInt number = BigInt.zero;
for (int byte in bytes) {
number = (number << 8) | BigInt.from(byte & 0xFF);
}
int bitLength = bytes.length * 8;
if (bitLength == 0) return '';
double log2N = log(n) / log(2);
int outputLength = (bitLength / log2N).ceil().toInt();
List<int> digits = [];
BigInt base = BigInt.from(n);
while (number > BigInt.zero) {
digits.add((number % base).toInt());
number = number ~/ base;
}
while (digits.length < outputLength) {
digits.add(0);
}
digits = digits.reversed.toList();
StringBuffer sb = StringBuffer();
for (int digit in digits) {
sb.write(charset[digit]);
}
return sb.toString();
}