hashTree method

Uint8List hashTree(
  1. List<Uint8List> items
)

Implementation

Uint8List hashTree(List<Uint8List> items) {
  final hashes = items.map((i) => sha256Update(i)).toList();

  while (hashes.length > 1) {
    var i = 0; // hashes index
    var p = 0; // pointer
    while (i < hashes.length) {
      if (i == hashes.length - 1) {
        // Move the last "alone" leaf to the pointer
        hashes[p] = hashes[i];
        i += 1;
      } else {
        List<int> forConcat = [];
        forConcat.addAll(hashes[i]);
        forConcat.addAll(hashes[i + 1]);
        hashes[p] = sha256Update(forConcat.asUint8List());
        i += 2;
      }
      p += 1;
    }
    hashes.length = p;
  }

  return hashes[0];
}