Md5.java 1.48 KB
Newer Older
yuquan.zhu committed
1 2
package cn.timer.api.utils;

翁国栋 committed
3
import java.io.UnsupportedEncodingException;
yuquan.zhu committed
4
import java.math.BigInteger;
翁国栋 committed
5
import java.nio.charset.StandardCharsets;
yuquan.zhu committed
6 7 8
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

翁国栋 committed
9 10
import java.util.Base64;

邓实川 committed
11
//import org.apache.shiro.crypto.hash.SimpleHash;
yuquan.zhu committed
12 13

/**
翁国栋 committed
14 15 16 17 18
 *
* @ClassName: Md5
* @Description: TODO(Md5加密)
* @author TZQ chess222_com
* @date 2019年3月2日 下午12:34:50
yuquan.zhu committed
19 20 21 22
*
 */
public class Md5 {

邓实川 committed
23 24 25 26
//	public static String md5password(String name, String password) {
//		Object tokenCredentials = new SimpleHash("md5", password, name, 56).toHex();
//		return (String) tokenCredentials;
//	}
yuquan.zhu committed
27 28 29 30 31 32 33 34 35

	// 写一个md5加密的方法
	public static String md5(String plainText) {
		// 定义一个字节数组
		byte[] secretBytes = null;
		try {
			// 生成一个MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 对字符串进行加密
翁国栋 committed
36 37
			secretBytes=plainText.getBytes(StandardCharsets.ISO_8859_1);
			md.update(secretBytes);
yuquan.zhu committed
38 39 40 41 42 43 44 45 46 47
			// 获得加密后的数据
			secretBytes = md.digest();
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有md5这个算法!");
		}
		// 将加密后的数据转换为16进制数字
		String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
		// 如果生成数字未满32位,需要前面补0
		for (int i = 0; i < 32 - md5code.length(); i++) {
			md5code = "0" + md5code;
翁国栋 committed
48
		}
yuquan.zhu committed
49 50 51 52 53 54 55
		return md5code;
	}
	public static void main(String[] args) {

	}

}