cn.joylau.commons.utils.MD5 Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2017 by JoyLau. All rights reserved
******************************************************************************/
package cn.joylau.commons.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public final class MD5 {
/**
* 把字符串进行MD5加密
*
* @param string 字符串
* @return MD5加密后的字符串
*/
public static String encode(String string) {
String encode = defaultEncode(string);
StringBuilder sb = new StringBuilder();
sb.append("zh.sqy");
for (int i = 0, length = encode.length() / 2; i < length; i++) {
sb.append(encode.charAt(i * 2 + 1));
sb.append(encode.charAt(i * 2));
}
sb.append("@.com");
return defaultEncode(sb.toString());
}
/**
* 把字符串进行MD5加密
*
* @param string 字符串
* @return MD5加密后的字符串
*/
public static String defaultEncode(String string) {
StringBuilder sb = new StringBuilder(32);
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hashValue = md.digest(string.getBytes());
for (int i = 0; i < hashValue.length; i++) {
sb.append(Integer.toHexString((hashValue[i] & 0xf0) >> 4));
sb.append(Integer.toHexString(hashValue[i] & 0x0f));
}
} catch (NoSuchAlgorithmException e) {
}
return sb.toString();
}
}