md5、sha哈希加密简单工具类:
package com.cjh.java.tools;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 加密工具
* @author 陈嘉豪
*
*/
public final class EncryptionUtils {
private static final String ENCODE = "UTF-8";
private static final class AlgorithmInstants{
private static final String MD5 = "MD5";
private static final String SHA_1 = "SHA-1";
private static final String SHA_256 = "SHA-256";
private static final String SHA_512 = "SHA-512";
}
/**
* 将字符串用MD5算法加密
* @param str 需要加密的字符串
* @return 返回长度为32位的字符串
* @throws Exception
*/
public static String getStrByMD5(String str) throws Exception{
return getStrByAlgorithm(str, AlgorithmInstants.MD5, ENCODE);
}
/**
* 将字符串用MD5算法加密
* @param str 需要加密的字符串
* @param encode 指定编码,默认为UTF-8
* @return 返回长度为32位的字符串
* @throws Exception
*/
public static String getStrByMD5(String str,String encode) throws Exception{
if(encode==null || encode.replaceAll(" ", "").length()<=0){
encode="UTF-8";
}
return getStrByAlgorithm(str, AlgorithmInstants.MD5, encode);
}
/**
* 将字符串用SHA-1算法加密
* @param str 需要加密的字符串
* @return 返回长度为40位的字符串
* @throws Exception
*/
public static String getStrBySHA1(String str) throws Exception{
return getStrByAlgorithm(str, AlgorithmInstants.SHA_1, ENCODE);
}
/**
* 将字符串用SHA-1算法加密
* @param str 需要加密的字符串
* @param encode 指定编码,默认为UTF-8
* @return 返回长度为40位的字符串
* @throws Exception
*/
public static String getStrBySHA1(String str,String encode) throws Exception{
if(encode==null || encode.replaceAll(" ", "").length()<=0){
encode="UTF-8";
}
return getStrByAlgorithm(str, AlgorithmInstants.SHA_1, encode);
}
/**
* 将字符串用SHA256算法加密
* @param str 需要加密的字符串
* @return 返回长度为128位的字符串
* @throws Exception
*/
public static String getStrBySHA256(String str) throws Exception{
return getStrByAlgorithm(str, AlgorithmInstants.SHA_256, ENCODE);
}
/**
* 将字符串用SHA256算法加密
* @param str 需要加密的字符串
* @param encode 指定编码,默认为UTF-8
* @return 返回长度为128位的字符串
* @throws Exception
*/
public static String getStrBySHA256(String str,String encode) throws Exception{
if(encode==null || encode.replaceAll(" ", "").length()<=0){
encode="UTF-8";
}
return getStrByAlgorithm(str, AlgorithmInstants.SHA_256, encode);
}
/**
* 将字符串用SHA512算法加密
* @param str 需要加密的字符串
* @return
* @throws Exception
*/
public static String getStrBySHA512(String str) throws Exception{
return getStrByAlgorithm(str, AlgorithmInstants.SHA_512, ENCODE);
}
/**
* 将字符串用SHA512算法加密
* @param str 需要加密的字符串
* @param encode 指定编码,默认为UTF-8
* @return
* @throws Exception
*/
public static String getStrBySHA512(String str,String encode) throws Exception{
if(encode==null || encode.replaceAll(" ", "").length()<=0){
encode="UTF-8";
}
return getStrByAlgorithm(str, AlgorithmInstants.SHA_512, encode);
}
/**
* 根据加密算法进行加密
* @param str
* @param algorithmName
* @param ecode
* @return
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
private static String getStrByAlgorithm(String str,String algorithmName,String encode) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest messageDigest = MessageDigest.getInstance(algorithmName);
messageDigest.update(str.getBytes(encode));
return byteToHex( messageDigest.digest() );
}
/**
* 将字节转换成十六进制
* @param bytes
* @return
*/
private static String byteToHex(byte[] bytes){
if(bytes!=null){
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i=0;i<bytes.length;i++){
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length()==1){
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
return null;
}
}