package com.sunyard.chsm.controller;

import com.sunyard.chsm.model.R;
import com.sunyard.chsm.param.SymDecryptReq;
import com.sunyard.chsm.param.SymDecryptResp;
import com.sunyard.chsm.param.SymEncryptReq;
import com.sunyard.chsm.param.SymEncryptResp;
import com.sunyard.chsm.param.SymHmacCheckReq;
import com.sunyard.chsm.param.SymHmacCheckResp;
import com.sunyard.chsm.param.SymHmacReq;
import com.sunyard.chsm.param.SymHmacResp;
import com.sunyard.chsm.param.SymMacCheckReq;
import com.sunyard.chsm.param.SymMacCheckResp;
import com.sunyard.chsm.param.SymMacReq;
import com.sunyard.chsm.param.SymMacResp;
import com.sunyard.chsm.service.SymKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 对称运算类接口
 *
 * @author liulu
 * @since 2024/12/17
 */
@RestController
@RequestMapping("/sym")
public class SymKeyController {

    @Autowired
    private SymKeyService symKeyService;

    /**
     * 对称加密
     *
     * @param req
     * @return
     */
    @PostMapping("/encrypt")
    public R<SymEncryptResp> encrypt(@Valid @RequestBody SymEncryptReq req) {
        SymEncryptResp resp = symKeyService.encrypt(req);
        return R.data(resp);
    }

    /**
     * 对称解密
     *
     * @param req
     * @return
     */
    @PostMapping("/decrypt")
    public R<SymDecryptResp> decrypt(@Valid @RequestBody SymDecryptReq req) {
        SymDecryptResp resp = symKeyService.decrypt(req);
        return R.data(resp);
    }

    /**
     * 计算Hmac
     *
     * @param req
     * @return
     */
    @PostMapping("/hmac")
    public R<SymHmacResp> hmac(@Valid @RequestBody SymHmacReq req) {
        SymHmacResp resp = symKeyService.hmac(req);
        return R.data(resp);
    }

    /**
     * 验证Hmac
     *
     * @param req
     * @return
     */
    @PostMapping("/hmac/check")
    public R<SymHmacCheckResp> macCheck(@Valid @RequestBody SymHmacCheckReq req) {
        SymHmacCheckResp resp = symKeyService.hmacCheck(req);
        return R.data(resp);
    }

    /**
     * 计算Hmac
     *
     * @param req
     * @return
     */
    @PostMapping("/mac")
    public R<SymMacResp> mac(@Valid @RequestBody SymMacReq req) {
        SymMacResp resp = symKeyService.mac(req);
        return R.data(resp);
    }

    /**
     * 验证Hmac
     *
     * @param req
     * @return
     */
    @PostMapping("/mac/check")
    public R<SymMacCheckResp> macCheck(@Valid @RequestBody SymMacCheckReq req) {
        SymMacCheckResp resp = symKeyService.macCheck(req);
        return R.data(resp);
    }

}
