001package top.cenze.utils.crypt;
002
003import cn.hutool.core.util.CharsetUtil;
004import cn.hutool.core.util.ObjectUtil;
005import cn.hutool.crypto.digest.HMac;
006import cn.hutool.crypto.digest.HmacAlgorithm;
007
008/**
009 * @desc: HMAC
010 * @author: chengze
011 * @createByDate: 2023/12/13 14:22
012 */
013public class HMacUtil {
014    /**
015     * MD5 加密
016     * @param data          待加密数据
017     * @param secretKey     加密密钥
018     * @return
019     */
020    public static String MD5(String data, String secretKey) {
021        HMac mac = new HMac(HmacAlgorithm.HmacMD5, secretKey.getBytes());
022        return mac.digestBase64(data, CharsetUtil.CHARSET_UTF_8, true);
023    }
024
025    /**
026     * SHA1 加密
027     * @param data          待加密数据
028     * @param secretKey     加密密钥
029     * @return
030     */
031    public static String SHA1(String data, String secretKey) {
032        HMac mac = new HMac(HmacAlgorithm.HmacSHA1, secretKey.getBytes());
033        return mac.digestBase64(data, CharsetUtil.CHARSET_UTF_8, true);
034    }
035
036    /**
037     * SHA256 加密
038     * @param data          待加密数据
039     * @param secretKey     加密密钥
040     * @return
041     */
042    public static String SHA256(String data, String secretKey) {
043        HMac mac = new HMac(HmacAlgorithm.HmacSHA256, secretKey.getBytes());
044        return mac.digestBase64(data, CharsetUtil.CHARSET_UTF_8, true);
045    }
046
047    /**
048     * SHA384 加密
049     * @param data          待加密数据
050     * @param secretKey     加密密钥
051     * @return
052     */
053    public static String SHA384(String data, String secretKey) {
054        HMac mac = new HMac(HmacAlgorithm.HmacSHA384, secretKey.getBytes());
055        return mac.digestBase64(data, CharsetUtil.CHARSET_UTF_8, true);
056    }
057
058    /**
059     * SHA512 加密
060     * @param data          待加密数据
061     * @param secretKey     加密密钥
062     * @return
063     */
064    public static String SHA512(String data, String secretKey) {
065        HMac mac = new HMac(HmacAlgorithm.HmacSHA512, secretKey.getBytes());
066        return mac.digestBase64(data, CharsetUtil.CHARSET_UTF_8, true);
067    }
068
069    /**
070     * 验证 加密算法(默认:HmacAlgorithm.HmacSHA256)
071     * @param data              待加密数据
072     * @param secretKey         加密密钥
073     * @param hmacVerify        加密后数据
074     * @return
075     */
076    public static boolean verify(String data, String secretKey, String hmacVerify) {
077        return verify(data, secretKey, hmacVerify, HmacAlgorithm.HmacSHA256);
078    }
079
080    /**
081     * 验证
082     * @param data              待加密数据
083     * @param secretKey         加密密钥
084     * @param hmacVerify        加密后数据
085     * @param hmacAlgorithm     加密算法(默认:HmacAlgorithm.HmacSHA256)
086     * @return
087     */
088    public static boolean verify(String data, String secretKey, String hmacVerify, HmacAlgorithm hmacAlgorithm) {
089        if (ObjectUtil.isNull(hmacAlgorithm)) {
090            hmacAlgorithm = HmacAlgorithm.HmacSHA256;
091        }
092
093        String hmac = null;
094        switch (hmacAlgorithm) {
095            case HmacMD5:
096                hmac = MD5(data, secretKey);
097                break;
098            case HmacSHA1:
099                hmac = SHA1(data, secretKey);
100                break;
101            case HmacSHA256:
102                hmac = SHA256(data, secretKey);
103                break;
104            case HmacSHA384:
105                hmac = SHA384(data, secretKey);
106                break;
107            case HmacSHA512:
108                hmac = SHA512(data, secretKey);
109                break;
110        }
111
112        return hmac.equals(hmacVerify);
113    }
114}