dart-lang / core

This repository is home to core Dart packages.
https://pub.dev/publishers/dart.dev
BSD 3-Clause "New" or "Revised" License
10 stars 2 forks source link

Support store/restore state? #231

Open isoos opened 4 years ago

isoos commented 4 years ago

I have a use-case where I'd like to store the internal state of a Hash, and then later restore it to continue the hashing of the incoming stream (upload happens in chunks with unpredictable time gap in-between). Would it be possible to support that?

caipeng-hrv commented 3 years ago

The same needs

licy183 commented 3 years ago

Rather than store the state of hash, store the bytes may work. Hope the following code would be useful for you.

import 'dart:math';

import 'package:crypto/crypto.dart' as Crypto;

Stream<List<int>> _generateData() async* {
  var random = Random();
  for (var i = 0;; ++i) {
    // Simulate time-consuming operation
    await Future.delayed(Duration(seconds: random.nextInt(2)));
    // End this operation
    if (i == 10) return;
    yield Iterable.generate(random.nextInt(100), (_) => random.nextInt(255))
        .toList();
  }
}

void main(List<String> arguments) {
  _generateData().toList().then((value) {
    print(Crypto.md5.convert(
      // Add all the bytes to one List
      value.reduce(
        (value, element) => value..addAll(element),
      ),
    ));
  });
}

PS: I'm not a native speaker of English. If there are any mistakes, please correct me.