SHA+HEX消息摘要加密

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 消息摘要加密
 * <p>
 * SHA+Hex
 *
 * @author 
 *
 */
public abstract class ShaHex {
	
	/** 明文编码 */
	private static final String ENCODING = "UTF8";
	
	/** 加密算法 */
	private static final String PROVIDER = "SHA-1";
	
	private static final Logger LOG = LoggerFactory.getLogger(ShaHex.class);
	
	private ShaHex() {
		// do nothing
	}
	
	/**
	 * 获得明文摘要信息
	 * @param message
	 * @return 加密成功时,返回摘要信息,加密失败时直接返回明文。
	 */
	public static final String messageDigest(String message) {
		String digest = null;
		try {
			final byte[] plainText = message.getBytes(ENCODING);
			MessageDigest messageDigest = MessageDigest.getInstance(PROVIDER);
			messageDigest.update(plainText);
			digest = new String(HexUtils.encode(messageDigest.digest()));
		} catch (UnsupportedEncodingException e) {
			LOG.error("加密失败!", e);
			digest = message;
		} catch (NoSuchAlgorithmException e) {
			LOG.error("加密失败!", e);
			digest = message;
		}
		return digest;
	}
}
 

你可能感兴趣的:(sha)