Flutter:MD5加密解密哈希函数

Lasted 2019-12-28 20:22:58

纯Dart实现的一组加密哈希函数,支持以下哈希算法:

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

安装

1. 在配置文件 pubspec.yaml中添加依赖,如下所示:

dependencies:
  crypto: ^2.1.4

2. 命令行安装软件包:

pub get

flutter pub get

3. Import it

import 'package:crypto/crypto.dart';

使用

字符串MD5加密

import 'package:crypto/crypto.dart';
import 'dart:convert'; // for the utf8.encode method

void main() {
  var bytes = utf8.encode("foobar"); // data being hashed

  var digest = md5.convert(bytes);

  print("Digest as bytes: ${digest.bytes}");
  print("Digest as hex string: $digest");
}