md5 function

Future<String> md5(
  1. String text
)

Compute MD5 hash of text.

Implementation

Future<String> md5(String text) async {
  // Try md5sum first (Linux), then md5 (macOS)
  for (final cmd in ['md5sum', 'md5']) {
    try {
      final proc = await Process.run('sh', [
        '-c',
        'printf "%s" "\$1" | $cmd',
        '--',
        text,
      ], stdoutEncoding: utf8);
      if (proc.exitCode == 0) {
        final out = (proc.stdout as String).trim();
        if (cmd == 'md5sum') return out.split(' ').first;
        // macOS md5 outputs "MD5 (...) = <hash>" or just the hash
        final eqIdx = out.indexOf('= ');
        return eqIdx >= 0
            ? out.substring(eqIdx + 2).trim()
            : out.split(' ').last;
      }
    } catch (_) {}
  }
  return _hashViaOpenssl('md5', text);
}