java md5工具类分享
java md5工具类分享
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:importjavasecurityMessageDigest;importjavasecurityNoSuchAlgor...

复制代码 代码如下:

import javasecurityMessageDigest;

import javasecurityNoSuchAlgorithmException;

/**

* MD5工具类

*

* @author

* @version 0

*/

public class Md5Util {

/**

* Md

*

* @param value the value

* @return the string

*/

public static String md5(String value) {

try {

MessageDigest md = MessageDigestgetInstance("md5");

byte[] e = mddigest(valuegetBytes());

return toHex(e);

}

catch (NoSuchAlgorithmException e) {

eprintStackTrace();

return value;

}

}

/**

* Md

*

* @param bytes the bytes

* @return the string

*/

public static String md5(byte[] bytes){

try {

MessageDigest md = MessageDigestgetInstance("md5");

byte[] e = mddigest(bytes);

return toHex(e);

}

catch (NoSuchAlgorithmException e) {

e.printStackTrace();

return "";

}

}

/**

* To hex

*

* @param bytes the bytes

* @return the string

*/

private static String toHex(byte bytes[]){

StringBuilder hs = new StringBuilder();

String stmp ="";

for (int n = 0; n < byteslength; n++) {

stmp = IntegertoHexString(bytes[n] & 0xff);

if (stmplength() == 1)

hsappend("0")append(stmp);

else

hsappend(stmp);

}

return hstoString();

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Java学习
热门Java学习
编程开发子分类