Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart MD5 From String

Tags:

dart

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

like image 731
Putra Ardiansyah Avatar asked Dec 18 '17 14:12

Putra Ardiansyah


2 Answers

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.

like image 115
Michael Spiss Avatar answered Sep 21 '22 04:09

Michael Spiss


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); } 
like image 25
Putra Ardiansyah Avatar answered Sep 24 '22 04:09

Putra Ardiansyah