package com.zhiqiyun.open.router.config;

import com.dliyun.oap.framework.security.TransportSecurity;
import com.dliyun.oap.framework.security.algorithm.DES;
import com.zhiqiyun.open.core.models.app.AppKeyInfo;
import com.zhiqiyun.open.core.service.AppKeyInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class DesTransportSecurity implements TransportSecurity {

    @Autowired
    private AppKeyInfoService appKeyInfoService;

    @Override
    public String responseEncrypt(String appKey, String s) {
        if (StringUtils.isNotBlank(appKey)) {
            AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(appKey);
            s = DES.encrypt(appKeyInfo.getEncryptKey(), s);
        }
        return s;
    }

    @Override
    public String requestDecrypt(String appKey, String s) {
        if (StringUtils.isNotBlank(appKey)) {
            AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(appKey);
            s = DES.decrypt(appKeyInfo.getEncryptKey(), s);
        }
        return s;
    }
}
