xor function

Uint8List xor(
  1. List<int> aList,
  2. List<int> bList
)

Implementation

Uint8List xor(List<int> aList, List<int> bList) {
  final a = Uint8List.fromList(aList);
  final b = Uint8List.fromList(bList);

  if (a.lengthInBytes == 0 || b.lengthInBytes == 0) {
    throw ArgumentError.value(
        "lengthInBytes of Uint8List arguments must be > 0");
  }

  bool aIsBigger = a.lengthInBytes > b.lengthInBytes;
  int length = aIsBigger ? a.lengthInBytes : b.lengthInBytes;

  Uint8List buffer = Uint8List(length);

  for (int i = 0; i < length; i++) {
    int aa, bb;
    try {
      aa = a.elementAt(i);
    } catch (e) {
      aa = 0;
    }
    try {
      bb = b.elementAt(i);
    } catch (e) {
      bb = 0;
    }

    buffer[i] = aa ^ bb;
  }

  return buffer;
}