package com.yj.md5;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.util.DigestUtils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Test {
public static void main(String[] args) throws NoSuchAlgorithmException {
String md5 = getMd5("123456");
System.out.println("md5 = " + md5);
md5 = getMd5_2("123456");
System.out.println("md5 = " + md5);
}
/**
* 自己手动加密
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMd5(String str) throws NoSuchAlgorithmException {
// str = str + ":" + tokenExpiryTime + ":" + getKey();
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); //jdk自带的方式
byte[] digest = messageDigest.digest(str.getBytes());
return new String(Hex.encode(digest));
}
public static String getMd5_2(String str) {
//spring 封装的方式
String md5DigestAsHex = DigestUtils.md5DigestAsHex(str.getBytes());
return md5DigestAsHex;
}
}
Md5加密
最新推荐文章于 2023-12-08 09:58:49 发布