How to generate md5 hash from string?
import 'package:crypto/crypto.dart' as crypto; ///Generate MD5 hash generateMd5(String data) { var content = UTF8.encode(data); var md5 = crypto.md5; }
I have no idea what next to do
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With