feat(REQ-2040): 密钥对生成工具
This commit is contained in:
parent
1a62878686
commit
4506422fc9
@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 签名工具类
|
||||
@ -20,9 +21,9 @@ public interface SignatureUtilApi {
|
||||
/**
|
||||
* 生成公私钥对
|
||||
*
|
||||
* @param algorithm 算法,例如:SHA256withRSA
|
||||
* @param algorithm 算法,例如:RSA
|
||||
* @return 公私钥对
|
||||
*/
|
||||
@GetMapping("/api/signature/generateKeyPair")
|
||||
ApiResult<KeyPair> generateKeyPair(@RequestParam(value = "algorithm") String algorithm);
|
||||
ApiResult<Map<String,String>> generateKeyPair(@RequestParam(value = "algorithm") String algorithm);
|
||||
}
|
||||
|
||||
@ -3,11 +3,15 @@ package cn.axzo.nanopart.server.controller;
|
||||
import cn.axzo.framework.domain.ServiceException;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import cn.axzo.nanopart.api.SignatureUtilApi;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author chenwenjian
|
||||
@ -18,14 +22,17 @@ import java.security.KeyPair;
|
||||
@RestController
|
||||
public class SignatureUtilController implements SignatureUtilApi {
|
||||
@Override
|
||||
public ApiResult<KeyPair> generateKeyPair(String algorithm) {
|
||||
public ApiResult<Map<String, String>> generateKeyPair(String algorithm) {
|
||||
log.info("algorithm = {}", algorithm);
|
||||
KeyPair keyPair = null;
|
||||
Map<String, String> keyPair = new HashMap<>();
|
||||
try {
|
||||
keyPair = SecureUtil.generateKeyPair(algorithm);
|
||||
KeyPair generateKeyPair = SecureUtil.generateKeyPair(algorithm);
|
||||
keyPair.put("publicKey", Base64.encode(generateKeyPair.getPublic().getEncoded()));
|
||||
keyPair.put("privateKey", Base64.encode(generateKeyPair.getPrivate().getEncoded()));
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("密钥对生成失败");
|
||||
}
|
||||
log.info("keyPair = {}", JSONUtil.toJsonStr(keyPair));
|
||||
return ApiResult.ok(keyPair);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user