同步回家做家庭作业
This commit is contained in:
+17
-1
@@ -34,11 +34,20 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sunyard.chsm</groupId>
|
||||
<artifactId>chsm-common</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.33</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sunyard.chsm</groupId>
|
||||
<artifactId>chsm-params</artifactId>
|
||||
@@ -51,5 +60,12 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.sunyard.chsm;
|
||||
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.sdf.BCSdfApiService;
|
||||
import com.sunyard.chsm.sdf.SdfApiService;
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class PerformanceTest {
|
||||
|
||||
private static final long max = 100_000L;
|
||||
private static final SdfApiService service = new BCSdfApiService();
|
||||
private static final byte[] data = new byte[ 1024 ];
|
||||
|
||||
@SneakyThrows
|
||||
public static void main(String[] args) {
|
||||
|
||||
int numThreads = 16; // Number of threads to use
|
||||
Thread[] threads = new Thread[numThreads];
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < numThreads; i++) {
|
||||
threads[i] = new Thread(() -> {
|
||||
testSM4();
|
||||
});
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
|
||||
for ( int i =0 ; i < numThreads; i++ ) {
|
||||
threads[i].join();
|
||||
}
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
double tps = numThreads * max * 1000.0 / (endTime - startTime);
|
||||
System.out.println("SM4 TPS: " + String.format("%.02f", tps));
|
||||
|
||||
}
|
||||
|
||||
// private static void testSM2() {
|
||||
// long startTime = System.currentTimeMillis();
|
||||
// EccKey eccKey = service.genKeyPairEcc();
|
||||
//
|
||||
// for (long i = 0; i < max; i++) {
|
||||
//
|
||||
//
|
||||
// service.asym
|
||||
//
|
||||
// }
|
||||
//
|
||||
// long endTime = System.currentTimeMillis();
|
||||
//
|
||||
// System.out.println("Addition result: " + sum + ", Time taken: " + (endTime - startTime) + " ns");
|
||||
// }
|
||||
|
||||
private static void testSM4() {
|
||||
|
||||
byte[] key = service.generateRandom(16);
|
||||
for (long i = 0; i < max; i++) {
|
||||
byte[] enData = service.symEncrypt(KeyAlg.SM4, key, data);
|
||||
service.symDecrypt(KeyAlg.SM4, key, enData);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -15,11 +15,12 @@ public class WebServerApp {
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
SpringApplication.run(WebServerApp.class, args);
|
||||
long endTime = System.currentTimeMillis();
|
||||
log.info("---------------------WebServerApp 启动完成-------------------");
|
||||
log.info("启动耗时 " + (endTime - startTime) + " ms");
|
||||
PerformanceTest.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.sunyard.chsm.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 拦截执行异常,返回错误码和提示信息
|
||||
* @author Cheney
|
||||
* @since 2024/11/13
|
||||
*/
|
||||
@Slf4j
|
||||
public class ResHandler implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||
log.error("Error in call {}: {}", request.getRequestURI(), ex);
|
||||
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* 处理对称计算类请求
|
||||
*/
|
||||
|
||||
|
||||
package com.sunyard.chsm.controller;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
import com.sunyard.chsm.service.KeyInfoService;
|
||||
import com.sunyard.chsm.service.SYMEncryptService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import params.DTORes;
|
||||
import params.DTOSymReq;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/sym")
|
||||
public class SYMEncryptController {
|
||||
|
||||
@Autowired
|
||||
private KeyInfoService keyInfoService;
|
||||
|
||||
@Autowired
|
||||
private SYMEncryptService symEncryptService;
|
||||
|
||||
// 支持的算法
|
||||
private static final Set<String> SUPPORTED_ALG = new HashSet<>();
|
||||
|
||||
// 支持的模式
|
||||
private static final Set<String> SUPPORTED_MODE = new HashSet<>();
|
||||
|
||||
// 支持的填充方式
|
||||
private static final Set<String> SUPPORTED_PADDING = new HashSet<>();
|
||||
|
||||
static {
|
||||
SUPPORTED_ALG.add("SM4");
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/encrypt")
|
||||
@ResponseBody
|
||||
public DTORes encrypt(@RequestBody DTOSymReq req) {
|
||||
return doEnOrDe(true, req);
|
||||
}
|
||||
|
||||
@PostMapping("/decrypt")
|
||||
@ResponseBody
|
||||
public DTORes decrypt(@RequestBody DTOSymReq req) {
|
||||
return doEnOrDe(false, req);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并处理加密和解密逻辑
|
||||
* @param en true 为加密 false 为解密
|
||||
* @param req 请求数据
|
||||
* @return 返回数据
|
||||
*/
|
||||
private DTORes doEnOrDe(boolean en, DTOSymReq req){
|
||||
// 参数检查
|
||||
// 填充默认参数
|
||||
if ( null == req.getAppId() ) {
|
||||
throw new IllegalArgumentException("appId can't be null");
|
||||
}
|
||||
|
||||
// 算法检查
|
||||
if ( ! SUPPORTED_ALG.contains(req.getAlg()) ) {
|
||||
throw new IllegalArgumentException("alg must be SM4");
|
||||
}
|
||||
|
||||
// 模式检查
|
||||
if ( ! SUPPORTED_MODE.contains(req.getMode()) ) {
|
||||
throw new IllegalArgumentException("mode must be ECB/CBC");
|
||||
}
|
||||
|
||||
// 填充检查
|
||||
if ( ! SUPPORTED_PADDING.contains( req.getPadding() ) ) {
|
||||
throw new IllegalArgumentException("padding must be none or pkcs5");
|
||||
}
|
||||
|
||||
|
||||
// iv
|
||||
byte[] iv = null;
|
||||
if ( ! "ECB".equals(req.getMode()) ) {
|
||||
if ( null == req.getIv() ) {
|
||||
throw new IllegalArgumentException("iv can't be null");
|
||||
}
|
||||
iv = Base64.decode( req.getIv() );
|
||||
}
|
||||
|
||||
// data
|
||||
if ( null == req.getData() ) {
|
||||
throw new IllegalArgumentException("data can't be null");
|
||||
}
|
||||
byte[] data = Base64.decode( req.getData() );
|
||||
|
||||
// 功能执行
|
||||
if ( en ) {
|
||||
data = symEncryptService.symEncrypt(
|
||||
KeyAlg.of( req.getAlg() ),
|
||||
AlgMode.of( req.getMode() ),
|
||||
Padding.of( req.getPadding() ),
|
||||
req.getAppId(),
|
||||
req.getKeyId(),
|
||||
iv,
|
||||
data
|
||||
);
|
||||
} else {
|
||||
data = symEncryptService.symEncrypt(
|
||||
KeyAlg.of( req.getAlg() ),
|
||||
AlgMode.of( req.getMode() ),
|
||||
Padding.of( req.getPadding() ),
|
||||
req.getAppId(),
|
||||
req.getKeyId(),
|
||||
iv,
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
return DTORes.success(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.sunyard.chsm.service;
|
||||
|
||||
|
||||
import cn.hutool.core.util.HexUtil;
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
import com.sunyard.chsm.model.dto.KeyInfoDTO;
|
||||
import com.sunyard.chsm.model.entity.KeyRecord;
|
||||
import com.sunyard.chsm.sdf.SdfApiService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Service
|
||||
public class SYMEncryptService {
|
||||
@Autowired
|
||||
private SdfApiService sdf;
|
||||
|
||||
@Autowired
|
||||
private KeyInfoService keyInfoService;
|
||||
|
||||
@Autowired
|
||||
private KeyRecordService keyRecordService;
|
||||
|
||||
|
||||
public byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, Long appId , Long keyId, byte[] iv, byte[] data) {
|
||||
// 查询 key 的值
|
||||
KeyRecord key = keyRecordService.selectByKeyId(keyId);
|
||||
// TODO 没有检查 appId
|
||||
|
||||
|
||||
// 验证 key
|
||||
|
||||
|
||||
// 计算
|
||||
return sdf.symEncrypt(
|
||||
alg, mode, padding,
|
||||
HexUtil.decodeHex(key.getKeyData()),
|
||||
iv,
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package params;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class DTORes {
|
||||
// 错误码,0 代表无错误
|
||||
private int code = 0;
|
||||
// 提示信息
|
||||
private String msg = "执行成功";
|
||||
// 数据
|
||||
private Object data;
|
||||
|
||||
|
||||
public static DTORes success(Object data){
|
||||
return success(data, "执行成功");
|
||||
}
|
||||
|
||||
public static DTORes success(Object data, String msg){
|
||||
DTORes res = new DTORes(0, msg, data);
|
||||
return res;
|
||||
}
|
||||
|
||||
public static DTORes fail(int code, String msg){
|
||||
DTORes res = new DTORes(code, msg,null);
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package params;
|
||||
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DTOSymReq {
|
||||
|
||||
// 应用 Id
|
||||
private Long appId;
|
||||
|
||||
// 密钥 Id
|
||||
private Long keyId;
|
||||
|
||||
// 算法
|
||||
private String alg;
|
||||
|
||||
// 模式
|
||||
private String mode = "ECB";
|
||||
|
||||
// padding
|
||||
private String padding = Padding.NOPadding.getCode();
|
||||
|
||||
// iv 可以为 null,base64 编码
|
||||
private String iv;
|
||||
|
||||
// 计算数据 base64 编码
|
||||
private String data;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package params;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DTOSymRes {
|
||||
|
||||
// 应用 Id
|
||||
private String appId;
|
||||
|
||||
// 密钥 Id
|
||||
private String keyId;
|
||||
|
||||
// 算法
|
||||
private String alg;
|
||||
|
||||
// iv 可以为 null,base64 编码
|
||||
private String iv;
|
||||
|
||||
// 计算数据 base64 编码
|
||||
private String data;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
import com.sunyard.chsm.WebServerApp;
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(classes = WebServerApp.class)
|
||||
public class BCSdfApiServiceTest {
|
||||
|
||||
@Autowired
|
||||
private BCSdfApiService bcSdfApiService;
|
||||
|
||||
private byte[] data = new byte[1024];
|
||||
|
||||
// 对称加密解密测试
|
||||
@Test
|
||||
void testSymECB() {
|
||||
|
||||
new Random().nextBytes(data);
|
||||
|
||||
byte[] key = bcSdfApiService.genSymKey(KeyAlg.SM4);
|
||||
byte[] enData = bcSdfApiService.symEncrypt(KeyAlg.SM4, key, data);
|
||||
byte[] deData = bcSdfApiService.symDecrypt(KeyAlg.SM4, key, enData);
|
||||
|
||||
Assert.assertArrayEquals(data, deData);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSymCBC() {
|
||||
new Random().nextBytes(data);
|
||||
|
||||
byte[] iv = new byte[16];
|
||||
|
||||
byte[] key = bcSdfApiService.genSymKey(KeyAlg.SM4);
|
||||
byte[] enData = bcSdfApiService.symEncrypt(KeyAlg.SM4, AlgMode.CBC, key, iv, data);
|
||||
byte[] deData = bcSdfApiService.symDecrypt(KeyAlg.SM4, AlgMode.CBC, key, iv, enData);
|
||||
|
||||
Assert.assertArrayEquals(data, deData);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package sdf;
|
||||
import com.sunyard.chsm.WebServerApp;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.sdf.BCSdfApiService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(classes = WebServerApp.class)
|
||||
public class BCSdfApiServiceTest {
|
||||
|
||||
@Autowired
|
||||
private BCSdfApiService bcSdfApiService;
|
||||
|
||||
// 对称加密解密测试
|
||||
@Test
|
||||
void testSym() {
|
||||
byte[] data = new byte[128];
|
||||
new Random().nextBytes( data );
|
||||
|
||||
byte[] key = bcSdfApiService.genSymKey( KeyAlg.SM4 );
|
||||
byte[] enData = bcSdfApiService.symEncrypt( KeyAlg.SM4, key, data );
|
||||
byte[] deData = bcSdfApiService.symDecrypt( KeyAlg.SM4, key, enData );
|
||||
|
||||
Assert.assertArrayEquals( data, deData );
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user