package com.seer.teach.common.utils;

import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 数据加解密工具类
 */
public class SignUtils {

    private static final String encoding = "UTF-8";

    /**
     * aes加密
     *
     * @param content 加密内容
     * @return
     */

    public static String encrypt(String content,String secret) {
        try {
            byte[] byteContent = content.getBytes(encoding);
            byte[] enCodeFormat = secret.getBytes(encoding);
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = secret.getBytes(encoding);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] encryptedBytes = cipher.doFinal(byteContent);
            String base64 = base64Encode2String(encryptedBytes);
            return URLEncoder.encode(base64, encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 生成客户端签名：MD5(appKey + timestamp + appSecret)
     */
    public static String generateSign(String appKey, String appSecret, String timestamp) {
        try {
            String raw = appKey + timestamp + appSecret;
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(raw.getBytes(encoding));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new CommonException(ResultCodeEnum.SIGN_FAIL);
        }
    }

    /**
     * aes解密
     *
     * @param content aes加密的内容
     * @return
     */
    public static String decrypt(String content, String secret) {
        try {
            //URL解码
            content = URLDecoder.decode(content, encoding);
            byte[] encryptedBytes = base64Decode(content);
            byte[] enCodeFormat = secret.getBytes(encoding);
            SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
            byte[] initParam = secret.getBytes(encoding);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] result = cipher.doFinal(encryptedBytes);
            return new String(result, encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base64编码
     */
    private static String base64Encode2String(final byte[] input) {
        if (input == null || input.length == 0) return "";
        return Base64.getEncoder().encodeToString(input);
    }

    /**
     * base64解码
     */
    private static byte[] base64Decode(final String input) {
        if (input == null || input.length() == 0) return new byte[0];
        return Base64.getDecoder().decode(input);
    }

}

