package com.encrypt;
import java.security.MessageDigest;
public class SHA256 {
/**
* 对字符串加密,加密算法使用MD5,SHA-256,默认SHA-256
* @param strSRC
* 要加密的字符串
* @param encName
* 加密类型
*/
public static String encrypt(String strSRC,String encName){
String strDes = null;
MessageDigest md = null;
byte[] bt = strSRC.getBytes();
try {
if(encName == null || encName.equals("")){
encName = "SHA-256";
}
md = MessageDigest.getInstance(encName);
md.update(bt);
strDes = byte2Hex(md.digest());
} catch (Exception e) {
// TODO: handle exception
}
return strDes;
}
public static String byte2Hex(byte[] bts){
String des = "";
String tmp = null;
for(int i=0;i<bts.length;i++){
tmp = (Integer.toHexString(bts[i] & 0xff));
if(tmp.length() ==1){
des += "0";
}
des += tmp;
}
return des;
}
}