package com.ho.user.service.impl;

import com.ho.common.tools.util.AESEncryptUtil;
import com.ho.common.tools.util.RSAUtil;
import com.ho.user.service.EncryptComponent;
import com.ho.user.vo.req.LoginReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author fancl
 * @desc:
 * @date 2022/11/17
 */
@Component
@Slf4j
public class EncryptComponentImpl implements EncryptComponent {

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

    /**
     * @param loginReqVO 带密文的对象
     * @return 解密后的对象
     */
    @Override
    public LoginReqVO decryptSecret(LoginReqVO loginReqVO) {
        String aesKey = RSAUtil.decode(loginReqVO.getAesKey(), privateKey);
        String iv = RSAUtil.decode(loginReqVO.getIv(), privateKey);
        if (loginReqVO.getPassword() != null) {
            String passwd = AESEncryptUtil.decrypt(loginReqVO.getPassword(), aesKey, iv);
            loginReqVO.setPassword(passwd);
        }
        loginReqVO.setAesKey(aesKey);
        loginReqVO.setIv(iv);

        log.info("解密后的 aesKey:{}, iv: {}", loginReqVO.getAesKey(), loginReqVO.getIv());
        return loginReqVO;
    }
}
