decode static method
Takes in a base32
string and decodes it back to a Uint8List that can be
converted to a hex string using Crypto.bytesToHex().
Implementation
static Uint8List decode(String base32) {
int index = 0, lookup, offset = 0, digit;
Uint8List bytes = new Uint8List(base32.length * 5 ~/ 8);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = 0;
}
for (int i = 0; i < base32.length; i++) {
lookup = base32.codeUnitAt(i) - '0'.codeUnitAt(0);
if (lookup < 0 || lookup >= _base32Lookup.length) {
continue;
}
digit = _base32Lookup[lookup];
if (digit == 0xFF) {
continue;
}
if (index <= 3) {
index = (index + 5) % 8;
if (index == 0) {
bytes[offset] |= digit;
offset++;
if (offset >= bytes.length) {
break;
}
} else {
bytes[offset] |= digit << (8 - index);
}
} else {
index = (index + 5) % 8;
bytes[offset] |= (digit >> index);
offset++;
if (offset >= bytes.length) {
break;
}
bytes[offset] |= digit << (8 - index);
}
}
return bytes;
}