java md5工具类分享
程序员文章站
2024-02-24 11:49:58
复制代码 代码如下:import javasecuritymessagedigest;import javasecuritynosuchalgorithmexception...
复制代码 代码如下:
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使用ftp上传文件示例分享
下一篇: Python中的浮点数原理与运算分析