首页 > 其他 > 详细

SSM整合项目 MD5加密工具类

时间:2021-05-12 14:47:07      阅读:36      评论:0      收藏:0      [点我收藏+]

SSM整合项目 MD5加密工具类

com.lyc.util.CrowdUtil.java

public class CrowdUtil {
    /**
     * 对明文字符串进行md5加密
     * @param source 传入的字符串
     * @return 加密的结果
     */
    public static String md5(String source) {
        // 1.判断source是否有效
        if (source == null || source.length() == 0) {
            // 2.如果不是有效的字符串抛出异常
            throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE);
        }
        try {
            // 3.获取MessageDigest对象
            String algorithm = "md5";
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

            // 4.获取明文字符串对应的字节数组
            byte[] input = source.getBytes();

            // 5.执行加密
            byte[] output = messageDigest.digest(input);

            // 6.创建BigInteger对象
            int signum = 1;
            BigInteger bigInteger = new BigInteger(signum,output);

            // 7.按照16进制将bigInteger的值转换为字符串
            int radix = 16;
            String encoded= bigInteger.toString(radix).toUpperCase();

            return encoded;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }
}

测试代码:

public class StringTest {
    @Test
    public void testMd5(){
        String source = "123123";
        String encoded = CrowdUtil.md5(source);
        System.out.println(encoded);
    }
}

运行结果:

技术分享图片

SSM整合项目 MD5加密工具类

原文:https://www.cnblogs.com/liuyunche/p/14758543.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!