com.tmsps.ne4spring.utils.SHAEncrypt Maven / Gradle / Ivy
package com.tmsps.ne4spring.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.digest.DigestUtils;
/**
*
* @author zhangwei [email protected]
*
*/
public class SHAEncrypt {
public static String shax(String shax, String strSrc) {
MessageDigest md = null;
String strDes = null;
byte[] bt = strSrc.getBytes();
try {
md = MessageDigest.getInstance(shax);
md.update(bt);
strDes = bytes2Hex(md.digest()); // to HexString
} catch (NoSuchAlgorithmException e) {
return null;
}
return strDes;
}
public static String bytes2Hex(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;
}
public static void main(String[] args) {
System.err.println(DigestUtils.sha256Hex("admin"));
System.err.println(SHAEncrypt.shax("SHA-256", "admin"));
}
}