crypto icon indicating copy to clipboard operation
crypto copied to clipboard

Support store/restore state?

Open isoos opened this issue 5 years ago • 2 comments

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?

isoos avatar Oct 05 '20 20:10 isoos

The same needs

caipeng-hrv avatar Nov 06 '20 02:11 caipeng-hrv

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.

licy183 avatar Apr 18 '21 17:04 licy183