首页 > 其他 > 详细

MD5

时间:2019-02-13 13:47:16      阅读:144      评论:0      收藏:0      [点我收藏+]
package com.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	private static Logger logger = LoggerFactory.getLogger(MD5.class);

	private static char[] hexs = {
			‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
			‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘
	};

    public static String encode(String source)
    {
        try {
			char[] result;
			MessageDigest digester = MessageDigest.getInstance("MD5");
			byte[] sbs = source.getBytes("UTF8");
			digester.update(sbs);
			byte[] rbs = digester.digest();
			int j = rbs.length;
			result = new char[j * 2];
			int k = 0;
			for(int i = 0; i < j; i++)
			{
			    byte b = rbs[i];
			    result[k++] = hexs[b >>> 4 & 15];
			    result[k++] = hexs[b & 15];
			}

			return new String(result);
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(),e);
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		}
        return null;
    }


}

 

MD5

原文:https://www.cnblogs.com/cuixiaomeng/p/10369375.html

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