package com.xinelu.web.controller.fd.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import com.tzwy.hmac.sm3.utils.Sm3HmacUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Author mengkuiliang
 * @Description 德州市居民码签名工具类
 * @Date 2023-06-08 16:45
 * @Param
 * @return
 **/
@Slf4j
@Component
public class DzsCodeSign {

    /**
     * @return java.lang.String
     * @Author mengkuiliang
     * @Description 生成签名
     * @Date 2023-06-08 16:40
     * @Param [appId, appSecret, isWhole]
     **/
    public static String createSign(String appId, String appSecret, String isWhole, String message) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmss");
        String str;
        if(isWhole.equals("0")) {
            str = appId + "&" + formater.format(new Date()) + "&" + getRandomString(10);
        } else {
            // message前边没有 &
            str = appId + "&" + formater.format(new Date()) + "&" + getRandomString(10) + message;
        }
        System.out.println("签名字符串：" + str);
        return makeSign(appSecret, str);
    }

    /**
     * @return boolean
     * @Author mengkuiliang
     * @Description 验证签名
     * @Date 2023-06-08 16:43
     * @Param [str, appSecret, sign]
     **/
    public static boolean verifySign(String str, String appSecret, String sign) {
        String signS = makeSign(appSecret, str);
        return signS.equals(sign);
    }

    /**
     * 生成指定长度的随机字符串
     */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成签名
     */
    public static String makeSign(String appSecret, String str) {
        try {
            byte[] sm3Sign = Sm3HmacUtils.calcMac(HexUtil.decodeHex(appSecret), str.getBytes("UTF-8"));
            return Base64.encode(sm3Sign);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {

        String appSecret = "74cb380c19b5448da4d7a1ef505089c4";
        String appid = "lhw8570wl9zojyou6zv6";
//        String time = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
        String time = "20230609165045";
//        String nonceStr = DzsCodeSign.getRandomString(10);
        String nonceStr = "8EqtkddxC2";
        String str = appid + "&" + time + "&" + nonceStr;
        String signS = makeSign(appSecret, str);
        System.out.println(signS);

        // JAlMiqpak3hwtTa+OzlWPcTuE2sSwn+F/I0j5/Cu3Mg=
    }
}
