package com.ho.user.controller;

import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.DataResult;
import com.ho.user.api.vo.req.DecryptReqVO;
import com.ho.common.tools.util.RSAUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fancl
 * @desc: 密钥相关Controller
 * @date 2022/11/14
 */
@RestController
@RequestMapping(ContextConstant.SYS + "key")
@Api(tags = "系统模块-密钥管理")
@Slf4j
public class KeyController {

    @Value("${rsa.pubKey}")
    private String rsaPubKey;

    @Value("${rsa.privateKey}")
    private String privateKey;

    @GetMapping("getPublicKey")
    @ApiOperation(value = "获取公钥")
    @TokenIgnore
    public DataResult getPublicKey() {
        log.info("获取密钥");
        return DataResult.success(rsaPubKey);

    }

    @ApiOperation(hidden = true, value = "解密测试")
    @RequestMapping("decrypt")
    public DataResult decrypt(@RequestBody DecryptReqVO decryptReqVO) {
        log.info("encryptText: {}", decryptReqVO);
        String s = RSAUtil.decode(decryptReqVO.getEncryptText(), privateKey);

        return DataResult.success();

    }


}
