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

class TokenMgr {
  //私有构造函数
  TokenMgr._internal();

  //保存单例
  static TokenMgr _singleton = new TokenMgr._internal();

  //工厂构造函数
  factory TokenMgr() => _singleton;

  String getSign(Map parameter) {
    String k = '';
    var keyList = [];
    for (var key in parameter.keys) {
      keyList.add(key);
    }
    keyList.sort();

    for (var key in keyList) {
      k += '$key=${parameter[key]}&';
    }
    k += 'key=O@MlSxWaeAlc5CYu';

    print(k);
    return generateMd5(k);
  }

  String generateMd5(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }
}
