decodeUniversalKey method

Uint8List decodeUniversalKey(
  1. String k
)

Implementation

Uint8List decodeUniversalKey(String k) {
  // A 32-character string drawn entirely from the key alphabet is a raw
  // UTF-8 key (Filen's "32-char key" convention), not base64. Anchor the
  // match to the whole string — a loose `contains` would treat almost any
  // 32-char base64 blob as a raw key.
  if (k.length == 32 && RegExp(r'^[A-Za-z0-9\-_]+$').hasMatch(k)) {
    return Uint8List.fromList(utf8.encode(k));
  }
  try {
    return base64Url.decode(base64Url.normalize(k));
  } catch (_) {}
  try {
    return base64.decode(base64.normalize(k));
  } catch (_) {}
  try {
    return Uint8List.fromList(HEX.decode(k));
  } catch (_) {}
  throw Exception('Key decode failed');
}