Convert String to MD5 with Flutter

Asked

Viewed 1,848 times

2

I have an application in Flutter,and I’m trying to convert my Password to MD5 on the login screen, I found some other methods but none served me. I’m trying to use final md5 = new MD5._() to convert final String password;, but without success. Would there be some other way to encrypt the String using the package Crypto flutter?

  • Hello, before anything, start by doing the tour to understand how the community works; then read the How to ask to check how you can improve your question and finally access help center to check various articles that will help you better understand the site.

1 answer

4


You can use the lib Crypto. It has support for hash algorithms:

  • SHA-1
  • SHA-256
  • MD5
  • HMAC (i.e. HMAC-MD5, HMAC-SHA1, HMAC-SHA256)

Just add in your pubspec.yaml:

crypto: ^2.0.6

Import the packages:

import 'dart:convert';
import 'package:crypto/crypto.dart';

And now the code:

String textToMd5 (String text) {
  return md5.convert(utf8.encode(text)).toString();
}

The md5.convert receives an int list, so we need to encode the string first with the utf8.encode.

In the lib link you find examples with other hashs.

  • 1

    Thank you very much Julio, it worked correctly so. I just called the textToMd5 in my Map toMap() {&#xA; var map = new Map<String, dynamic>();&#xA; map["password"] = textToMd5(password);&#xA; return map;&#xA; }

  • 2

    Show! Just a hint, don’t use the 'new' reserved word for creating the objects, from Dart 2 it has become optional, and it is now a 'default' of the community not to use it. Facilitates reading of code.

  • Great, thanks for the tip, I’ll put in practice from now on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.