Java MD5Utils
/**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved.</p>
* <p> Created on 19941115</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.alpaca.framework.utils.encrypt;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.StringUtils;
/**
* @Package:cn.ucaner.alpaca.framework.utils.encrypt
* @ClassName:MD5Utils
* @Description: <p> MD5加密工具 By Jason </p>
* @Author: - Jason
* @CreatTime:2018年5月24日 下午9:40:31
* @Modify By:
* @ModifyTime: 2018年5月24日
* @Modify marker:
* @version V1.0
*/
class MD5Utils {
protected final static String MD5_KEY = "MD5";
protected final static String SHA_KEY = "SHA1";
/**
* @param value
* @param key
* @return
*/
protected static String encrypt(String value,String key) {
try {
// 拿到一個MD5轉換器(如果想要SHA1參數換成”SHA1”)
MessageDigest messageDigest = MessageDigest.getInstance(key);
// 輸入的字符串轉換成字節數組
byte[] inputByteArray = value.getBytes();
// inputByteArray是輸入字符串轉換得到的字節數組
messageDigest.update(inputByteArray);
// 轉換並返回結果,也是字節數組,包含16個元素
byte[] resultByteArray = messageDigest.digest();
// 字符數組轉換成字符串返回
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
/**
* 字節數組轉換為hex
* @param byteArray
* @return
*/
private static String byteArrayToHex(byte[] byteArray) {
// 首先初始化一個字符數組,用來存放每個16進制字符
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F' };
// new一個字符數組,這個就是用來組成結果字符串的(解釋一下:一個byte是八位二進制,也就是2位十六進制字符(2的8次方等於16的2次方))
char[] resultCharArray = new char[byteArray.length * 2];
// 遍歷字節數組,通過位運算(位運算效率高),轉換成字符放到字符數組中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
// 字符數組組合成字符串返回
return new String(resultCharArray);
}
/**
* 獲得16位的加密字符
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMd5String16(String str) throws NoSuchAlgorithmException {
String md5str = getMd5String32(str).substring(8);
return md5str.substring(0, md5str.length() - 8);
}
/**
* 獲得24位的MD5加密字符
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMd5String24(String str) throws NoSuchAlgorithmException {
String md5str = getMd5String32(str).substring(4);
return md5str.substring(0, md5str.length() - 4);
}
/**
* 獲得32位的MD5加密算法
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMd5String32(String str) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer();
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString();
}
/**
* 獲取MD5密碼
* @param password
* @param salt
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMD5Pwd(String password, String salt) throws NoSuchAlgorithmException {
String result = null;
if (StringUtils.isNotBlank(salt)) {
result = getMD5(getMD5(password) + salt);
} else {
result = getMD5(password);
}
return result;
}
/**
* 獲取MD5加密數據
* @param input
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMD5(String input) throws NoSuchAlgorithmException {
String result = input;
if (input != null) {
MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
md.update(input.getBytes());
BigInteger hash = new BigInteger(1, md.digest());
result = hash.toString(16);
while (result.length() < 32) {//40 for SHA-1
result = "0" + result;
}
}
return result;
}
/**
* For test by Jason
*/
public static void main(String[] args) {
try {
System.out.println(getMd5String16("Jason")); //829018f9dbd65fb8
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
