博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MD5Utils 简单计算MD5
阅读量:7043 次
发布时间:2019-06-28

本文共 3512 字,大约阅读时间需要 11 分钟。

import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.util.HashMap;import java.util.Map;import java.util.concurrent.locks.ReentrantLock;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * 简单计算MD5 */public class MD5Utils {    private static final Log               log     = LogFactory.getLog(MD5Utils.class);    private static char[]                  digits  = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',            'd', 'e', 'f'                         };    private static Map
rDigits = new HashMap
(16); static { for (int i = 0; i < digits.length; ++i) { rDigits.put(digits[i], i); } } private static MD5Utils me = new MD5Utils(); private MessageDigest mHasher; private ReentrantLock opLock = new ReentrantLock(); private MD5Utils(){ try { mHasher = MessageDigest.getInstance("md5"); } catch (Exception e) { throw new RuntimeException(e); } } public static MD5Utils getInstance() { return me; } public String getMD5String(String content) { return bytes2string(hash(content)); } public String getMD5String(byte[] content) { return bytes2string(hash(content)); } public byte[] getMD5Bytes(byte[] content) { return hash(content); } /** * 对字符串进行md5 * * @param str * @return md5 byte[16] */ public byte[] hash(String str) { opLock.lock(); try { byte[] bt = mHasher.digest(str.getBytes("UTF-8")); if (null == bt || bt.length != 16) { throw new IllegalArgumentException("md5 need"); } return bt; } catch (UnsupportedEncodingException e) { throw new RuntimeException("unsupported utf-8 encoding", e); } finally { opLock.unlock(); } } /** * 对二进制数据进行md5 * * @param str * @return md5 byte[16] */ public byte[] hash(byte[] data) { opLock.lock(); try { byte[] bt = mHasher.digest(data); if (null == bt || bt.length != 16) { throw new IllegalArgumentException("md5 need"); } return bt; } finally { opLock.unlock(); } } /** * 将一个字节数组转化为可见的字符串 * * @param bt * @return */ public String bytes2string(byte[] bt) { int l = bt.length; char[] out = new char[l << 1]; for (int i = 0, j = 0; i < l; i++) { out[j++] = digits[(0xF0 & bt[i]) >>> 4]; out[j++] = digits[0x0F & bt[i]]; } if (log.isDebugEnabled()) { log.debug("[hash]" + (new String(out))); } return new String(out); } /** * 将字符串转换为bytes * * @param str * @return byte[] */ public byte[] string2bytes(String str) { if (null == str) { throw new IllegalArgumentException("str is null"); } if (str.length() != 32) { throw new IllegalArgumentException("str.length() != 32"); } byte[] data = new byte[16]; char[] chs = str.toCharArray(); for (int i = 0; i < 16; ++i) { int h = rDigits.get(chs[i * 2]).intValue(); int l = rDigits.get(chs[i * 2 + 1]).intValue(); data[i] = (byte) ((h & 0x0F) << 4 | (l & 0x0F)); } return data; }}

转载地址:http://lhhal.baihongyu.com/

你可能感兴趣的文章
wcf 请考虑增加操作超时
查看>>
【设计模式】简单工厂模式
查看>>
[LeetCode] Binary Tree Paths 二叉树路径
查看>>
对JAVA集合进行遍历删除时务必要用迭代器
查看>>
poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
查看>>
统计多少个汉字与字母
查看>>
Odoo9发行说明
查看>>
logging日志管理--将日志打印在屏幕上
查看>>
6个理由告诉你为什么要用NAS
查看>>
使用EPEL和REMI第三方yum源
查看>>
时间单位的档案
查看>>
细数人们对安卓的误解
查看>>
PF_NETLINK应用实例NETLINK_KOBJECT_UEVENT具体实现--udev实现原理
查看>>
mongodb 3.x 之实用新功能窥看[2] ——使用$lookup做多表关联处理
查看>>
实际利率 > 名义利率
查看>>
Odoo Xml Datetime 类型显示为 Date类型
查看>>
remove-duplicates-from-sorted-list
查看>>
SQLAchemy Core学习之Reflection
查看>>
内存调试工具Electric Fence
查看>>
2015年11月系统架构设计师案例分析题
查看>>