package com.bootdo.datas.tools.gm;

import org.bouncycastle.util.encoders.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bootdo.common.config.BootdoConfig;

/**
 * @date 2022/4/21 18:04
 * @author wangp
 * @description 国密能力
 */
@Component
public class GmUtils {
    private static Logger log = LoggerFactory.getLogger(GmUtils.class);

    @Autowired
    private BootdoConfig bootdoConfig;

    /**
     * @Description: 加密
     * @Author: wangp
     * @Date: 2022/4/21 18:05
     * @param
     * @Return: String
     */
    public String encrypt(String value) {
        byte[] sourceData = value.getBytes();
        log.info("Crt:" + bootdoConfig.getCrtPrival());
        byte[] encData = FileCryptTool.encryptData(bootdoConfig.getCrtPrival(), sourceData);
        String encValue = "";
        if (encData != null) {
            encValue = Hex.toHexString(encData);
        }
        log.info("encValue:" + encValue);
        return encValue;
    }

    /**
     * @Description: 解密
     * @Author: wangp
     * @Date: 2022/4/21 18:05
     * @param
     * @Return: String
     */
    public String decrypt(String value) {
        byte[] decData = FileCryptTool.decryptData(bootdoConfig.getEncPrival(), Hex.decode(value));
        String decValue = "";
        if (decData != null) {
            decValue = new String(decData);
        }
        log.info("decValue:" + decValue);
        return decValue;
    }
}
