/**
* a b c d e f 也可以改成大写的 A B C D E F
*/
private static final char[] CHAR_ARRAY = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String generateMD5(String str) {
if (str == null) {
throw new NullPointerException("Parameters is null");
}
if ("".equals(str)) {
throw new RuntimeException("Empty string");
}
MessageDigest msgDigest;
try {
msgDigest = MessageDigest.getInstance("MD5");
msgDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("No such algorithm exception");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding exception");
}
byte[] bytes = msgDigest.digest();
char[] out = new char[16 * 2];
int len = 16;
for (int i = 0, j = 0; i < len; i++) {
out[j++] = CHAR_ARRAY[bytes[i] >>> 4 & 0xf];
out[j++] = CHAR_ARRAY[bytes[i] & 0xf];
}
return new String(out);
}
Java MD5加密
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Android、Java在http请求的时候都会做两端加密验证,有的会使用AES加密、解密; 但大部分都是使用到M...
- MD5加密MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法...