package com.jsowell.thirdparty.lianlian.util;

import com.jsowell.common.constant.Constants;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.DateUtils;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Locale;

/**
 * 联联平台工具类
 *
 * @author JS-ZZA
 * @date 2023/5/6 14:22
 */
public class LianLianUtils {
    static final String ALGORITHM_MAC = "HmacMD5";

    /** 密钥 **/
    static final String MAC_KEY = "TmsdVaFVTtjzZbLi";


    /**
     * HMAC加密
     * @return
     * @throws Exception
     */
    public static String encryptionHMAC(String source) throws Exception {
        SecretKey secretKey = new SecretKeySpec(MAC_KEY.getBytes("UTF-8"), ALGORITHM_MAC);
        Mac mac = Mac.getInstance(ALGORITHM_MAC);
        mac.init(secretKey);
        mac.update(source.getBytes("UTF-8"));
        return BytesUtil.binary(mac.doFinal(), 16).toUpperCase(Locale.ROOT);
    }

    /**
     * 拼接参数
     * @param Data
     * @return
     */
    public static String connectData(String Data){
        String timeStamp = DateUtils.dateTimeNow(DateUtils.YYYYMMDDHHMMSS);
        String number = "0001";
        return Constants.OPERATORID_LIANLIAN + Data + timeStamp + number;
    }
}
