MD5加密工具类

 

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 * @author lyn
 */
public class Md5Utils {

    /**
     * MD5加密
     *
     * @param str
     * @return
     */
    public final static String encode(String str) {
        try {
            //创建具有指定算法名称的摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            //使用指定的字节数组更新摘要
            md.update(str.getBytes());
            //进行哈希计算并返回一个字节数组
            byte mdBytes[] = md.digest();
            String hash = "";
            //循环字节数组
            for (int i = 0; i < mdBytes.length; i++) {
                int temp;
                //如果有小于0的字节,则转换为正数
                if (mdBytes[i] < 0) {
                    temp = 256 + mdBytes[i];
                } else {
                    temp = mdBytes[i];
                }

                if (temp < 16) {
                    hash += "0";
                }

                //将字节转换为16进制后,转换为字符串
                hash += Integer.toString(temp, 16);
            }
            return hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String encodeWithSalt(String numStr, String salt) {
        return encode(encode(numStr) + salt);
    }

    public static void main(String[] args) {
        System.out.println(encode("test"));
        System.out.println(encodeWithSalt("123456", "gyhfis"));
    }
}

 

posted @ 2022-03-08 18:55  进击的小蔡鸟  阅读(197)  评论(0编辑  收藏  举报