java根据字符串生成md5,根据字节生成
根据字符串生成
[Java] 纯文本查看 复制代码 import java.security.MessageDigest;
/**
* Md5 工具
*/
public class Md5Util {
private static MessageDigest md5 = null;
static {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 用于获取一个String的md5值
* @param string
* @return
*/
public static String getMd5(String str) {
byte[] bs = md5.digest(str.getBytes());
StringBuilder sb = new StringBuilder(40);
for(byte x:bs) {
if((x & 0xff)>>4 == 0) {
sb.append("0").append(Integer.toHexString(x & 0xff));
} else {
sb.append(Integer.toHexString(x & 0xff));
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(getMd5("hello world"));
}
}
根据字节生成
[Java] 纯文本查看 复制代码 import java.security.MessageDigest;
/**
* Md5 工具
*/
public class Md5Util {
private static MessageDigest md5 = null;
static {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 用于获取一个String的md5值
* @param string
* @return
*/
public static String getMd5(byte[] setbs) {
byte[] bs = md5.digest(setbs);
StringBuilder sb = new StringBuilder(40);
for(byte x:bs) {
if((x & 0xff)>>4 == 0) {
sb.append("0").append(Integer.toHexString(x & 0xff));
} else {
sb.append(Integer.toHexString(x & 0xff));
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(getMd5("hello world"));
}
}
|