package top.baogutang.strategy.utils;

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

public class SignUtil {
    public static String sign(long timestamp, String secret) {
        try {
            String str = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
            byte[] signData = mac.doFinal(str.getBytes(StandardCharsets.UTF_8));
            String base64 = Base64.getEncoder().encodeToString(signData);
            return URLEncoder.encode(base64, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
