sha256HashFile function

Future<String?> sha256HashFile(
  1. String filePath
)

Computes the SHA256 hash of a file using certutil (Windows).

Returns null if certutil is unavailable or the hash cannot be parsed.

Implementation

Future<String?> sha256HashFile(String filePath) async {
  try {
    final result = await Process.run(
      'certutil',
      ['-hashfile', filePath, 'SHA256'],
      runInShell: true,
    );
    if (result.exitCode != 0) return null;
    final lines =
        (result.stdout as String).split('\n').map((l) => l.trim()).toList();
    for (final line in lines) {
      if (RegExp(r'^[a-fA-F0-9]{64}$').hasMatch(line)) return line;
    }
    return null;
  } catch (_) {
    return null;
  }
}