package com.bootdo.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * ant 加密 工具类
 *
 * @author bc
 * @Date 2020-08-12 9:34
 */
public class EncryptUtil {

    protected final static Charset CHARSET_UTF8 = Charset.forName("utf-8");

    private final static String SECRET_ID = "SecretId";

    private final static String BODY = "Body";

    private final static String TIME_STAMP = "Timestamp";

    private final static String SIGNVERSION = "Signversion";

    private final static String MAC_NAME = "HmacSHA1";

    private final static String EQUALS_SYMBOL = "=";

    private final static String AND = "&";

    private final static Base64 base64 = new Base64();

    /**
     * 生成签名
     *
     * @param timestamp 时间戳
     * @param secretId 申请的secretId
     * @param secretKey 申请的secretKey
     * @param contentJsonString 待加密的字符串
     * @param method 方法名
     * @return 生成的验签签名
     */
    public static String signature(String timestamp, String secretId, String secretKey, String contentJsonString,String method) throws Exception {

        String stringBuilder = toSignSourceSimplePost(timestamp, secretId, contentJsonString);
        System.out.println("stringBuilder:"+stringBuilder);
        byte[] bytes = hmacSHA1Encrypt(stringBuilder, secretKey);
        return base64.encodeToString(bytes);
    }

    /**
     * 简单拼接顺序字符串
     */
    private static String toSignSourceSimplePost(String timestamp, String secretId, String jsonStr) {
        StringBuilder builder = new StringBuilder();
        builder.append(SECRET_ID).append(EQUALS_SYMBOL).append(secretId).append(AND);
        builder.append(TIME_STAMP).append(EQUALS_SYMBOL).append(timestamp).append(AND);
        builder.append(SIGNVERSION).append(EQUALS_SYMBOL).append("v2").append(AND);
        builder.append(BODY).append(EQUALS_SYMBOL).append(jsonStr);
        return builder.toString();
    }

    private static String toSignSourceSimpleGet(String timestamp, String secretId, String jsonStr) {
        StringBuilder builder = new StringBuilder();
        builder.append(SECRET_ID).append(EQUALS_SYMBOL).append(secretId).append(AND);
        builder.append(TIME_STAMP).append(EQUALS_SYMBOL).append(timestamp).append(AND);
        builder.append(SIGNVERSION).append(EQUALS_SYMBOL).append("v2");
        return builder.toString();
    }

    /**
     * 加密原始参数
     *
     * @param encryptText 待加密的原始字符串
     * @param encryptKey 颁发的secretKey
     * @return 加密的byte数组，用于生成签名
     */
    private static byte[] hmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
        //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
        SecretKey secretKey = new SecretKeySpec(encryptKey.getBytes(CHARSET_UTF8), MAC_NAME);
        //生成一个指定 Mac 算法 的 Mac 对象
        Mac mac = Mac.getInstance(MAC_NAME);
        //用给定密钥初始化 Mac 对象
        mac.init(secretKey);

        byte[] data = encryptText.getBytes(CHARSET_UTF8);
        if (null == data || 0 == data.length){
            return data;
        }
        if(data.length>1024){
            data = Arrays.copyOfRange(data,0,1024);
        }

        //完成 Mac 操作
        return mac.doFinal(data);
    }

    private static String key = "SecretId=J5HrA2KccOexV0IWx8I&Timestamp=1597659585761&Body={\n" +
            "  \"account\": \"string\",\n" +
            "  \"cameraId\": \"string\",\n" +
            "  \"cameraName\": \"string\",\n" +
            "  \"cameraSerialNum\": \"string\",\n" +
            "  \"deviceType\": \"string\",\n" +
            "  \"groupId\": \"string\",\n" +
            "  \"ip\": \"string\",\n" +
            "  \"lat\": \"string\",\n" +
            "  \"lng\": \"string\",\n" +
            "  \"password\": \"string\",\n" +
            "  \"port\": \"string\",\n" +
            "  \"rtsp\": \"string\",\n" +
            "  \"suppleRtsp\": \"string\"\n" +
            "}";


}
