Java获取字符串16进制hash值

Java获取字符串16进制hash值

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

public class MD5Util {
private static MD5Util mInstance = null;

public static MD5Util getInstance() {
	if (mInstance == null) {
	mInstance = new MD5Util();
	}
	return mInstance;
}

/**
* 获取字符串16进制hash值
* @param input
* @return
*/
public String toHashHexStr(String input) {
	if (input == null || input.equals("")) {
	return null;
	}
	try {
	MessageDigest messageDigest = MessageDigest.getInstance("MD5");
	// 输入的字符串转换成字节数组
	byte[] inputByteArray = input.getBytes();
	// inputByteArray是输入字符串转换得到的字节数组
	messageDigest.update(inputByteArray);
	// 转换并返回结果,也是字节数组,包含16个元素
	byte[] resultByteArray = messageDigest.digest();
	// 字符数组转换成字符串返回
	return byteArrayToHex(resultByteArray);
	} catch (NoSuchAlgorithmException e) {
	e.printStackTrace();
	return null;
	}
}

private String byteArrayToHex(byte[] resultByteArray) {
	String hs = "";
	String stmp = "";
	for (int n = 0; n < resultByteArray.length; n++) {
	stmp = (java.lang.Integer.toHexString(resultByteArray[n] & 0XFF));
	if (stmp.length() == 1)
	hs = hs + "0" + stmp;
	else
	hs = hs + stmp;
	}
	return hs.toUpperCase();
}
}

原创内容转载请保留出处GEEK笔记(http://www.geekapp.cn/)。

原创博客,转载请标明出处:http://www.geekapp.cn/archives/151.html
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇