package com.citygis.web.dataEncrypt.service.impl;

import com.citygis.web.dataEncrypt.service.DataEncryptService;
import com.citygis.web.dataEncrypt.utils.CipherMachineUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: zyl
 * @CreateTime: 2025-02-19
 * @Description:
 * @Version: 1.0
 */
@Service
public class DataEncryptServiceImpl implements DataEncryptService {

    @Value("${cipherMachine.pwd}")
    private String pwd;

    @Value("${cipherMachine.generateTokenUrl}")
    private String generateTokenUrl;

    @Value("${cipherMachine.encryptDataUrl}")
    private String encryptDataUrl;

    @Value("${cipherMachine.decryptDataUrl}")
    private String decryptDataUrl;

    @Value("${cipherMachine.paddingType}")
    private String paddingType;

    @Value("${cipherMachine.keyIndex}")
    private Integer keyIndex;

    @Value("${cipherMachine.algType}")
    private String algType;

    @Value("${cipherMachine.iv}")
    private String iv;

    @Value("${cipherMachine.hmacAlgType}")
    private String hmacAlgType;

    @Value("${cipherMachine.hmacUrl}")
    private String hmacUrl;

    @Override
    public String dataEncrypt(String plainText) throws Exception {
        String token = CipherMachineUtil.generateToken(generateTokenUrl, pwd);
        String encryptedBase64 = CipherMachineUtil.encryptData(token, plainText, encryptDataUrl, paddingType, keyIndex, algType, iv);
        return encryptedBase64;
    }

    @Override
    public String dataDecrypt(String encryptedBase64) throws Exception {
        String token = CipherMachineUtil.generateToken(generateTokenUrl, pwd);
        String decryptedText = CipherMachineUtil.decryptData(token, encryptedBase64, decryptDataUrl, paddingType, keyIndex, algType, iv);
        return decryptedText;
    }

    @Override
    public String generateHmac(String originalData) throws Exception {
        String token = CipherMachineUtil.generateToken(generateTokenUrl, pwd);
        String generateHmac = CipherMachineUtil.generateHmac(hmacUrl, token, originalData, keyIndex, hmacAlgType);
        return generateHmac;
    }
}
