Skip to content

Instantly share code, notes, and snippets.

@diaolizhi
Created July 16, 2019 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diaolizhi/12c0aee7d61894362fd49f351c11477a to your computer and use it in GitHub Desktop.
Save diaolizhi/12c0aee7d61894362fd49f351c11477a to your computer and use it in GitHub Desktop.
常用工具类:生成 UUID,对 String 进行 MD5 加密
import java.security.MessageDigest;
import java.util.UUID;
/**
* 常用工具类的封装,md5,uuid等
*/
public class CommonUtils {
/**
* 生成 uuid, 即用来标识一笔单,也用做 nonce_str
* @return
*/
public static String generateUUID(){
String uuid = UUID.randomUUID().toString().
replaceAll("-","").substring(0,32);
return uuid;
}
/**
* md5常用工具类
* @param data
* @return
*/
public static String MD5(String data){
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte [] array = md5.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment