package com.usky.issue.service.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * HMAC 签名工具
 *
 * @author fyc
 * @date 2026-06-08
 */
public final class HmacSignUtil {

    private static final String HMAC_SHA256 = "HmacSHA256";

    private HmacSignUtil() {
    }

    public static String sign(String content, String secret) {
        try {
            Mac mac = Mac.getInstance(HMAC_SHA256);
            mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), HMAC_SHA256));
            byte[] hash = mac.doFinal(content.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(hash);
        } catch (Exception e) {
            throw new IllegalStateException("HMAC签名失败", e);
        }
    }
}
