HMAC-MD5比MD5多一个key,我们用到的是对Map加密,所以需要对Map里的字段进行解析拼接
trim.dart
class SJTrim {
static List<T> trimList<T>(List<T> list) {
return (null != list) ? list : List<T>();
}
static Map<K, V> trimMap<K, V>(Map<K, V> map) {
return (null != map) ? map : Map<K, V>();
}
static int trimInt(int value, [int defaultValue = 0]) {
return (null != value) ? value : defaultValue;
}
static bool trimBool(bool value, [bool defaultValue = false]) {
return (null != value) ? value : defaultValue;
}
static double trimDouble(double value, [double defaultValue = 0]) {
return (null != value) ? value : defaultValue;
}
static String trimString(String value, [String defaultValue = '']) {
return (null != value) ? value : defaultValue;
}
}
trackinfo_singer.dart
class SJTrackInfoSinger {
final _trackMapInfo = SplayTreeMap<String, String>();
void addInt(String key, int value) {
if (0 == SJTrim.trimInt(value)) {
return;
}
_trackMapInfo[key] = '$value';
}
void addString(String key, String value) {
if (0 == SJTrim.trimString(value).length) {
return;
}
_trackMapInfo[key] = value;
}
String toStringInfo() {
final stringBuffer = StringBuffer();
_trackMapInfo.forEach((key, value) {
stringBuffer.write(value);
});
return stringBuffer.toString();
}
}
调用
String generateMD5(SJRouteInfoV4 routInfo) {
final singer = SJTrackInfoSinger();
singer.addInt('sj', routInfo.sj);
if (routInfo.point != null && routInfo.point.length > 0) {
final stringBuffer = StringBuffer();
routInfo.point.forEach((location) {//routInfo.point是list,location是map类型
final singerTmp = SJTrackInfoSinger();
singerTmp.addString('speed', location.speed);
singerTmp.addInt('locationType', location.locationType);
stringBuffer.write(singerTmp.toStringInfo());
});
singer.addString('point', stringBuffer.toString());
}
final string = singer.toStringInfo();
final encryptKey = utf8.encode('88888888');
final ecnryptData = utf8.encode(string);
final hmacSha256 = new Hmac(sha256, encryptKey);
final digest = hmacSha256.convert(ecnryptData);
return digest.toString();
}