package com.hr.util;

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

import static com.hr.util.ByteFormat.bytesToHexString;


/**
 * Author: y
 * Date: 2019/7/22 15:58
 * <p>
 * Description: HmacSHA256加密工具
 */
public class HmacSHA256 {
    /**
     * sha256_HMAC加密
     * @param message 消息
     * @param secret  秘钥
     * @return 加密后字符串
     */
    public static String sha256_HMAC(String message, String secret) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
            hash = bytesToHexString(bytes);
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return hash;
    }

}