sha256 function

Future<String> sha256(
  1. String text
)

Compute SHA-256 hash of text.

Uses the shasum command on macOS/Linux for simplicity without requiring a crypto package.

Implementation

Future<String> sha256(String text) async {
  final result = await Process.run(
    'shasum',
    ['-a', '256'],
    stdoutEncoding: utf8,
    stderrEncoding: utf8,
    environment: Platform.environment,
  );
  // Fallback: use openssl
  if (result.exitCode != 0) {
    return _hashViaOpenssl('sha256', text);
  }
  // Use printf + shasum via shell
  final proc = await Process.run('sh', [
    '-c',
    'printf "%s" \${1} | shasum -a 256',
    '--',
    text,
  ], stdoutEncoding: utf8);
  if (proc.exitCode == 0) {
    return (proc.stdout as String).split(' ').first.trim();
  }
  return _hashViaOpenssl('sha256', text);
}