前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >md5 java 工具类_javamd5工具类

md5 java 工具类_javamd5工具类

作者头像
全栈程序员站长
发布2022-09-14 11:37:28
2930
发布2022-09-14 11:37:28
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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;

//import sun.security.provider.MD5;

/**

* 简单计算MD5

*

* @author agapple 2015年3月26日 下午8:45:47

* @since 5.1.19

*/

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;

}

}

以上代码就是用于md5的算法

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159015.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档