1 MD5简介
信息-摘要算法(Message-digest Algorithm 5,MD5)于90年代初由MIT Labortory for computer sicence和RSA data security inc的ronald l.rivest开发,经md2、md3和md4发展而来。MD5的典型应用是对一段信息(message)产生信息摘要(message-digest),防止信息被篡改,常应用于加密和解密技术、Unix系统中。
2 Usage
2.1 Java原始生成MD5
package basic.datatype.dataprocess;
import java.security.MessageDigest;
public class MD5SecurityUtil {
public static String genMD5ByRaw(String str){
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(str.getBytes("UTF-8"));
String md5Str = "";
for(int i=0;i<digest.length;i++){
md5Str += Integer.toHexString((0x000000FF&digest[i])|0xFFFFFF00).substring(6);
}
return md5Str;
}catch(Exception e){
e.printStackTrace();
}
return "ERROR";
}
public static void main(String[] args){
String md5ByRaw = genMD5ByRaw("str");
System.out.println("md5 by raw:"+md5ByRaw);
}
}
2.2 Java数学处理生成MD5
package basic.datatype.dataprocess;
import java.security.MessageDigest;
import java.math.BigInteger;
public class MD5SecurityUtil {
public static String genMD5ByMath(String str){
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(str.getBytes());
// byte[] digest = md5.digest(str.getBytes("utf-8"));
String md5Str = new BigInteger(1, md5.digest()).toString(16);
return md5Str;
}catch(Exception e){
e.printStackTrace();
}
return "ERROR";
}
public static void main(String[] args){
String md5 = genMD5ByMath("str");
System.out.println("md5 by math:"+md5);
}
}
2.3 Spring生成MD5
package com.company.workordersystem.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import java.util.Map;
import java.util.HashMap;
@Service
@Transactional
public class UNPlusService {
@Value("${unplus.url}")
private String unplusUrl;
public String genMD5(String currentTime){
String SID = "WY_YT";
String spliceData = unplusUrl + currentTime + SID;
String md5 = DigestUtils.md5DigestAsHex(spliceData.getBytes());
return md5;
}
public Map submitWorkorderToUNPlus(Map reqCont, Map svcCont){
Map<String, String> returnMap = new HashMap<>();
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("ReqCont", reqCont);
bodyMap.put("SvcCont", svcCont);
bodyMap.put("optType", "addWorkInfo");
return bodyMap;
// return returnMap;
}
}
【参考文献】
[1]https://blog.csdn.net/qq_30683329/article/details/80879058
[2]https://www.cnblogs.com/chinaifae/p/10188535.html
[3]https://blog.csdn.net/junmoxi/article/details/80841555