Dart MD5 From String
Just to save anyone else a few minutes of searching, to hash/digest a file from a stream you can use:
final fileStream = File('path/to/file.txt').openRead();
final checksum = (await md5.bind(fileStream).first).toString();
Found an answer:
import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart' as crypto;
///Generate MD5 hash
generateMd5(String data) {
var content = new Utf8Encoder().convert(data);
var md5 = crypto.md5;
var digest = md5.convert(content);
return hex.encode(digest.bytes);
}
A more efficient approach would be the following:
import 'dart:convert';
import 'package:crypto/crypto.dart';
String generateMd5(String input) {
return md5.convert(utf8.encode(input)).toString();
}
This avoids loading the unnecessary convert
package and uses the already existing Utf8Codec
, instead of instantiating a Utf8Encoder
object each time the function is called.