MD5 工具类
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Util {
private Md5Util() {
}
private static final String ALGORITHM_NAME = "MD5";
private static final char[] HEX_CHAR_ARRAY = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_CHAR_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_CHAR_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
public static final byte[] digest(byte[] content) {
try {
MessageDigest md5 = MessageDigest.getInstance(ALGORITHM_NAME);
return md5.digest(content);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static final String digestHex(byte[] content) {
return bytesToHex(digest(content));
}
}
网友评论