12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package com.jkcredit.utils;
- import java.security.MessageDigest;
- /**
- * @description: MD5加密
- * @author:
- * @create: 2019-05-14 11:05
- * @version: V1.0
- **/
- public class Md5Utils {
- /**
- * 生成md5,全部大写
- * @param message
- * @return
- */
- public static String md5(String message) {
- try {
- // 1 创建提供信息摘要算法的对象,初始化为md5算法对象
- MessageDigest md = MessageDigest.getInstance("MD5");
- // 2 将消息变成byte数组
- byte[] input = message.getBytes();
- // 3 计算后获得字节数,这就是那128位了
- byte[] buff = md.digest(input);
- // 4 把数组每字节(一个字节占八位)换16进制连成md5字符
- return byte2hex(buff);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 二进制转十六进制字符
- * @param bytes
- * @return
- */
- private static String byte2hex(byte[] bytes) {
- StringBuilder sign = new StringBuilder();
- for (int i = 0; i < bytes.length; i++) {
- String hex = Integer.toHexString(bytes[i] & 0xFF);
- if (hex.length() == 1) {
- sign.append("0");
- }
- sign.append(hex.toUpperCase());
- }
- return sign.toString();
- }
- }
|