package com.ruoyi.web.controller.chenyanlogin.huicunutil;

import cn.hutool.core.util.StrUtil;
import org.apache.http.util.Asserts;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

public class HuiCunJiaMi {
    /**
     * 加密算法
     */
    private final static String ALGORITHM_AES = "AES";

    /**
     * 算法/模式/补码方式
     */
    private final static String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding";

    /**
     * 安全策略
     */
    private final static String SHA1_SECURITY_POLICY = "SHA1PRNG";

    /**
     * AES密钥长度
     */
    public static final int AES_KEY_LENGTH = 16;

    /**
     * AES密钥可能的长度
     */
    private static final List<Integer> AES_KEY_POSSIBLE_LENGTH = Arrays.asList(16, 24, 32);

    /**
     * 加密
     *
     * @param secretKey
     * @param content
     * @return
     */
    public static String AESEncode(String secretKey, String content) {
        // 判断密钥是否为空
        if (StrUtil.isEmpty(secretKey)) {
            System.out.println("密钥为空");
        }
        // 判断Key是否为16位
        if (secretKey.length() != AES_KEY_LENGTH) {
             System.out.println("密钥长度不是16位");
        }
        try {
            byte[] raw = secretKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM_AES);
            // "算法/模式/补码方式"
            Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            // 此处使用BASE64做转码功能，同时能起到2次加密的作用。
//            String encode = new BASE64Encoder().encode(encrypted);
            String encode = Base64.getEncoder().encodeToString(encrypted);
            encode = encode.replaceAll(System.getProperty("line.separator"), "");
            return encode;
        } catch (Exception e) {

             System.out.println("AES加密异常");
        }
        return null;
    }

    /**
     * 解密
     *
     * @param secretKey
     * @param content
     * @return
     */
    public static String AESDecode(String secretKey, String content) {
        // 判断Key是否为空
        if (StrUtil.isEmpty(secretKey)) {
             System.out.println("密钥为空");
        }
        // 判断Key是否为16位
        if (secretKey.length() != AES_KEY_LENGTH) {
             System.out.println("密钥长度不是16位");
        }
        try {
            byte[] raw = secretKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM_AES);
            Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
//            byte[] encrypted = new BASE64Decoder().decodeBuffer(content);
            byte[] encrypted = Base64.getDecoder().decode(content);
            byte[] original = cipher.doFinal(encrypted);
            String originalString = new String(original, StandardCharsets.UTF_8);
            originalString = originalString.replaceAll(System.getProperty("line.separator"), "");
            return originalString;
        } catch (Exception e) {

             System.out.println("AES解密异常");
        }
        return null;
    }


    /**
     * 生成签名，进行客户端签名校验
     **/
    private static String getSign(String token, Long timestamp, String appId, String appKey, String appSecret) {
        String str = String.format("%s:%s:%s:%s:%s", token, String.valueOf(timestamp), appId, appKey, appSecret);
        String encodestr = SignUtils.sha256ByText(str);
        return encodestr;
    }

    /**
     * 生成返回签名，用于客户端进行校验
     **/
    private static String getSignBakV2(String token, Long timestamp, String appId, String appKey, String appSecret, String phone, String areaCode) {
        String str = String.format("%s:%s:%s:%s:%s:%s:%s", token, String.valueOf(timestamp), appId, appKey, appSecret, phone, areaCode);
        String encodestr = SignUtils.sha256ByText(str);
        return encodestr;
    }
}
