Java SHA加解密工具类

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Hex;

/**
 * SHA加解密工具类
 */
public final class SHAEncryptUtil {

	public static final String DEFAULT_CHARSET = "UTF-8";
	public static final String ALGORITHM_SHA_1 = "SHA-1";
	public static final String ALGORITHM_SHA_256 = "SHA-256";
	public static final String ALGORITHM_HMAC_SHA_256 = "HmacSHA256";
	public static final String ALGORITHM_HMAC_SHA_512 = "HmacSHA512";
	public static final String ALGORITHM_SHA_512 = "SHA-512";

	/**
	 * SHA-1 加密
	 * @param strText
	 * @return 加密结果
	 */
	public static String sha1(final String strText) {
		return sha(strText, ALGORITHM_SHA_1);
	}

	/**
	 * SHA256 加密
	 * @param strText
	 * @return 加密结果
	 */
	public static String sha256(final String strText) {
		return sha(strText, ALGORITHM_SHA_256);
	}
	
	/**
	 * HmacSHA256 加密
	 * @param strText
	 * @param key
	 * @return
	 */
	public static String hmacSha256(final String strText,final String key) {
		return hmacSha256Encrypt(strText, key,ALGORITHM_HMAC_SHA_256);
	}

    /**
     * HmacSHA512 加密
     * @param strText
     * @param key
     * @return
     */
    public static String hmacSha512(final String strText,final String key) {
        return hmacSha512Encrypt(strText, key,ALGORITHM_HMAC_SHA_512);
    }

	/**
	 * SHA512 加密
	 * @param strText
	 * @return
	 */
	public static String sha512(final String strText) {
		return sha(strText, ALGORITHM_SHA_512);
	}

	/**
	 * 算法加密
	 * @param strText
	 * @param algorithm
	 * @return 加密结果
	 */
	private static String sha(final String strText, final String algorithm) {
		String strResult = null;
		if (strText != null && strText.length() > 0) {
			try {
				MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
				messageDigest.update(strText.getBytes());
				byte byteBuffer[] = messageDigest.digest();
				StringBuffer strHexString = new StringBuffer();
				for (int i = 0; i < byteBuffer.length; i++) {
					String hex = Integer.toHexString(0xff & byteBuffer[i]);
					if (hex.length() == 1) {
						strHexString.append('0');
					}
					strHexString.append(hex);
				}
				strResult = strHexString.toString();
			} catch (NoSuchAlgorithmException e) {
				throw new RuntimeException(e);
			}
		}
		return strResult;
	}
	
	public static String hmacSha256Encrypt(String content, String key,String algorithm) {
		try {
			Mac hmac256 = Mac.getInstance(algorithm);
			SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(DEFAULT_CHARSET), algorithm);
			hmac256.init(secretKey);
			return Hex.encodeHexString(hmac256.doFinal(content.getBytes(DEFAULT_CHARSET)));
		} catch (Exception e) {
			throw new RuntimeException(algorithm + " 算法加密失败", e);
		}
	}

    public static String hmacSha512Encrypt(String content, String key,String algorithm) {
        try {
            Mac hmac512 = Mac.getInstance(algorithm);
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(DEFAULT_CHARSET), algorithm);
            hmac512.init(secretKey);
            return Hex.encodeHexString(hmac512.doFinal(content.getBytes(DEFAULT_CHARSET)));
        } catch (Exception e) {
            throw new RuntimeException(algorithm + " 算法加密失败", e);
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值