computeFileHash method

  1. @override
Stream<FileHashProgress> computeFileHash(
  1. String filePath
)
override

Computes SHA256 hash of a file by reading it in chunks Returns stream updates with progress and a final event containing FileHashProgress.hash

Implementation

@override
Stream<FileHashProgress> computeFileHash(String filePath) async* {
  final file = await _getFile(filePath);
  if (file == null) {
    throw Exception('No file selected or file not found');
  }

  final totalBytes = file.size;

  final digestSink = _DigestSink();
  final input = sha256.startChunkedConversion(digestSink);

  int processedBytes = 0;
  yield FileHashProgress(
    processedBytes: processedBytes,
    totalBytes: totalBytes,
  );

  await for (var chunk in readFileAsStream(filePath)) {
    input.add(chunk);
    processedBytes += chunk.length;
    yield FileHashProgress(
      processedBytes: processedBytes,
      totalBytes: totalBytes,
    );
  }

  input.close();

  yield FileHashProgress(
    processedBytes: totalBytes,
    totalBytes: totalBytes,
    isComplete: true,
    hash: digestSink.value?.toString(),
  );
}