base64To16ByteKey function

Uint8List base64To16ByteKey(
  1. String base64Key
)

Converts a Base64-encoded string to a 16-byte key.

This function decodes the provided Base64 string into bytes and ensures that the resulting byte array is exactly 16 bytes long. If the decoded byte array is already 16 bytes, it is returned as is. If it is longer than 16 bytes, it is truncated to 16 bytes. If it is shorter than 16 bytes, it is padded with zeros to reach 16 bytes.

  • Parameter base64Key: The Base64-encoded string to be converted.
  • Returns: A 16-byte Uint8List derived from the Base64 string.

Implementation

Uint8List base64To16ByteKey(String base64Key) {
  final keyBytes = base64Decode(base64Key);
  if (keyBytes.length == 16) {
    return keyBytes; // Already 16 bytes
  } else if (keyBytes.length > 16) {
    return keyBytes.sublist(0, 16); // Truncate to 16 bytes
  } else {
    final paddedKey = Uint8List(16)
      ..setRange(0, keyBytes.length, keyBytes); // Pad with zeros
    return paddedKey;
  }
}