/**
* 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位子分组,经过了一系列的处理后,算法...