Compare commits
44
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c79aae14e8 | ||
|
|
cec433ad3b | ||
|
|
555da388ef | ||
|
|
dd44703a33 | ||
|
|
c8577571a6 | ||
|
|
374ddad9bc | ||
|
|
ecd2d6cfe1 | ||
|
|
55c00b6579 | ||
|
|
6cd738a172 | ||
|
|
2c8638a167 | ||
|
|
9c4c19d271 | ||
|
|
17a40377d7 | ||
|
|
18aa757871 | ||
|
|
a452376d8d | ||
|
|
7a4a83e6d0 | ||
|
|
b212a265ee | ||
|
|
c9a224f810 | ||
|
|
09f5516792 | ||
|
|
3965146c77 | ||
|
|
12b53a2e0f | ||
|
|
48d9719fbc | ||
|
|
882e84320c | ||
|
|
12bdac2d7c | ||
|
|
31f8c9155e | ||
|
|
09a910006f | ||
|
|
83e5033ba0 | ||
|
|
664bd110c6 | ||
|
|
b82002801d | ||
|
|
d6a27f3341 | ||
|
|
01be8429be | ||
|
|
799f3763d1 | ||
|
|
76809e5521 | ||
|
|
983dc8a47d | ||
|
|
0dc2d7c026 | ||
|
|
02fd07def2 | ||
|
|
10d9d1b0d6 | ||
|
|
b8322538e7 | ||
|
|
83802b6ef6 | ||
|
|
9062a9c17c | ||
|
|
88f4bdf603 | ||
|
|
385e77b766 | ||
|
|
5259876780 | ||
|
|
f7fa777677 | ||
|
|
1439f4aa63 |
@@ -14,3 +14,4 @@ src/test/java/com/sunyard/sydapi/sample/*.*
|
|||||||
~$*
|
~$*
|
||||||
/pack/
|
/pack/
|
||||||
/.logs/
|
/.logs/
|
||||||
|
/bin/
|
||||||
|
|||||||
+2
-2
@@ -50,8 +50,8 @@
|
|||||||
</options>
|
</options>
|
||||||
</error>
|
</error>
|
||||||
|
|
||||||
<pk len="32" cn="公钥"></pk>
|
<pk len="128" cn="公钥"></pk>
|
||||||
<sk len="160" cn="私钥"></sk>
|
<sk len="64" cn="私钥"></sk>
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,24 @@
|
|||||||
# sydapi4j
|
# sydapi4j
|
||||||
|
|
||||||
加密机 java 接口,基于 jna。
|
加密机 java 接口,基于 jna。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 启动 Slave
|
||||||
|
在作为负载机的 Slave 机器上进行配置,主要有两种方式:
|
||||||
|
|
||||||
|
方法一:配置 jmeter.properties 文件
|
||||||
|
编辑 $JMETER_HOME/bin/jmeter.properties 文件,确认或取消注释以下配置:
|
||||||
|
```
|
||||||
|
server.rmi.ssl.disable=true # 禁用SSL,简化内网配置
|
||||||
|
server.rmi.localport=1099 # 固定RMI端口,方便防火墙放行
|
||||||
|
java.rmi.server.hostname=Slave_IP # 绑定本机网卡IP,避免多网卡问题
|
||||||
|
```
|
||||||
|
|
||||||
|
配置完成后,在 Slave 的 bin 目录下启动服务:
|
||||||
|
|
||||||
|
Windows: 双击 jmeter-server.bat
|
||||||
|
Linux/Mac: 运行 ./jmeter-server
|
||||||
|
|
||||||
|
./jmeter-server -Djava.rmi.server.hostname=172.1.41.24 -Dserver.rmi.localport=1099
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
|
||||||
|
http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||||
|
<id>assembled-test-jar</id>
|
||||||
|
<formats>
|
||||||
|
<format>jar</format>
|
||||||
|
</formats>
|
||||||
|
<includeBaseDirectory>false</includeBaseDirectory>
|
||||||
|
<fileSets>
|
||||||
|
<!-- 打包主代码 -->
|
||||||
|
<fileSet>
|
||||||
|
<directory>${project.build.outputDirectory}</directory>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
</fileSet>
|
||||||
|
<!-- 打包测试代码 -->
|
||||||
|
<fileSet>
|
||||||
|
<directory>${project.build.testOutputDirectory}</directory>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
<!-- 打包依赖的Jar包 -->
|
||||||
|
<dependencySets>
|
||||||
|
<dependencySet>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
<unpack>true</unpack>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependencySet>
|
||||||
|
</dependencySets>
|
||||||
|
</assembly>
|
||||||
+3849
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<groupId>com.sunyard</groupId>
|
<groupId>com.sunyard</groupId>
|
||||||
<artifactId>sydapi4j</artifactId>
|
<artifactId>sydapi4j</artifactId>
|
||||||
<version>1.0.2022-SNAPSHOT</version>
|
<version>2.01</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>sydapi4j</name>
|
<name>sydapi4j</name>
|
||||||
@@ -32,28 +32,12 @@
|
|||||||
<version>1.0.3</version>
|
<version>1.0.3</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- JMeter Java Sampler 核心依赖 -->
|
||||||
|
|
||||||
<!-- Source: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bouncycastle</groupId>
|
<groupId>org.apache.jmeter</groupId>
|
||||||
<artifactId>bcprov-jdk15on</artifactId>
|
<artifactId>ApacheJMeter_java</artifactId>
|
||||||
<version>1.65</version>
|
<version>5.4.3</version>
|
||||||
<!-- <scope>compile</scope>-->
|
<scope>provided</scope>
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Source: https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk18on -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.bouncycastle</groupId>
|
|
||||||
<artifactId>bcpkix-jdk15on</artifactId>
|
|
||||||
<version>1.65</version>
|
|
||||||
<!-- <scope>compile</scope>-->
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.houbb</groupId>
|
|
||||||
<artifactId>junitperf</artifactId>
|
|
||||||
<version>1.0.3</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -72,8 +56,6 @@
|
|||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.zxing</groupId>
|
<groupId>com.google.zxing</groupId>
|
||||||
<artifactId>core</artifactId>
|
<artifactId>core</artifactId>
|
||||||
@@ -85,26 +67,16 @@
|
|||||||
<version>3.5.0</version>
|
<version>3.5.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.jmeter</groupId>
|
|
||||||
<artifactId>ApacheJMeter_java</artifactId>
|
|
||||||
<version>5.4.3</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bouncycastle</groupId>
|
<groupId>org.bouncycastle</groupId>
|
||||||
<artifactId>bcpkix-jdk15on</artifactId>
|
<artifactId>bcpkix-jdk15on</artifactId>
|
||||||
<version>1.68</version>
|
<version>1.68</version>
|
||||||
<!-- <scope>provided</scope>-->
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bouncycastle</groupId>
|
<groupId>org.bouncycastle</groupId>
|
||||||
<artifactId>bcprov-jdk15on</artifactId>
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
<version>1.68</version>
|
<version>1.68</version>
|
||||||
<!-- <scope>provided</scope>-->
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
|
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
|
||||||
@@ -126,29 +98,6 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.13.1</version>
|
<version>2.13.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- <dependency>-->
|
|
||||||
<!-- <groupId>org.codehaus.groovy</groupId>-->
|
|
||||||
<!-- <artifactId>groovy-all</artifactId>-->
|
|
||||||
<!-- <version>2.4.15</version>-->
|
|
||||||
<!--<!– <version>4.0.26</version>–>-->
|
|
||||||
<!-- </dependency>-->
|
|
||||||
<!-- <dependency>-->
|
|
||||||
<!-- <groupId>ch.qos.logback</groupId>-->
|
|
||||||
<!-- <artifactId>logback-classic</artifactId>-->
|
|
||||||
<!-- <version>1.3.15</version>-->
|
|
||||||
<!-- <scope>compile</scope>-->
|
|
||||||
<!-- </dependency>-->
|
|
||||||
|
|
||||||
<!--<dependency>-->
|
|
||||||
<!--<groupId>club.fullstack</groupId>-->
|
|
||||||
<!--<artifactId>serialize</artifactId>-->
|
|
||||||
<!--<version>[1.0.201806051035, 1.1.SNAPSHOT)</version>-->
|
|
||||||
<!--</dependency>-->
|
|
||||||
|
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/com.github.wvengen/proguard-maven-plugin -->
|
|
||||||
<!-- https://mvnrepository.com/artifact/com.github.wvengen/proguard-maven-plugin -->
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<finalName>sydapi4j</finalName>
|
<finalName>sydapi4j</finalName>
|
||||||
@@ -158,7 +107,6 @@
|
|||||||
<groupId>org.codehaus.groovy</groupId>
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
<artifactId>groovy-eclipse-compiler</artifactId>
|
<artifactId>groovy-eclipse-compiler</artifactId>
|
||||||
<version>2.9.2-01</version>
|
<version>2.9.2-01</version>
|
||||||
<!-- <version>3.5.0-01</version>-->
|
|
||||||
<extensions>true</extensions>
|
<extensions>true</extensions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
@@ -234,22 +182,6 @@
|
|||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<archive>
|
|
||||||
<manifest>
|
|
||||||
<addClasspath>true</addClasspath>
|
|
||||||
<classpathPrefix>lib/</classpathPrefix>
|
|
||||||
<mainClass>com.xxx.uploadFile</mainClass>
|
|
||||||
</manifest>
|
|
||||||
</archive>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-dependency-plugin</artifactId>
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
@@ -276,9 +208,12 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<artifactId>maven-assembly-plugin</artifactId>
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
<configuration>
|
<configuration>
|
||||||
|
<descriptors>
|
||||||
|
<descriptor>assembly.xml</descriptor>
|
||||||
|
</descriptors>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<mainClass>com.Sample.TestDemo</mainClass>
|
<mainClass>sample.TestDemo</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
<descriptorRefs>
|
<descriptorRefs>
|
||||||
|
|||||||
@@ -45,6 +45,12 @@ public class SydApiException extends RuntimeException{
|
|||||||
case 4:
|
case 4:
|
||||||
msg = "DER 编码格式错误(公钥) ";
|
msg = "DER 编码格式错误(公钥) ";
|
||||||
break;
|
break;
|
||||||
|
case 31:
|
||||||
|
msg = "缓冲区索引错误";
|
||||||
|
break;
|
||||||
|
case 0xd:
|
||||||
|
msg = "公钥加密错误";
|
||||||
|
break;
|
||||||
case 21:
|
case 21:
|
||||||
msg = "输入的数据格式错误 ";
|
msg = "输入的数据格式错误 ";
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
package com.sunyard.entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SM2加密解密时间统计类
|
||||||
|
* 用于记录发送、计算、接收三个阶段的耗时
|
||||||
|
*/
|
||||||
|
public class TimeStats {
|
||||||
|
// 发送阶段总耗时(毫秒)- 循环发送数据块的时间
|
||||||
|
private long sendTime;
|
||||||
|
// 计算阶段耗时(毫秒)- 执行加密/解密计算的时间
|
||||||
|
private long computeTime;
|
||||||
|
// 接收阶段总耗时(毫秒)- 循环接收数据块的时间
|
||||||
|
private long receiveTime;
|
||||||
|
// 发送次数
|
||||||
|
private int sendCount;
|
||||||
|
// 接收次数
|
||||||
|
private int receiveCount;
|
||||||
|
|
||||||
|
public TimeStats() {
|
||||||
|
this.sendTime = 0;
|
||||||
|
this.computeTime = 0;
|
||||||
|
this.receiveTime = 0;
|
||||||
|
this.sendCount = 0;
|
||||||
|
this.receiveCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getSendTime() {
|
||||||
|
return sendTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSendTime(long sendTime) {
|
||||||
|
this.sendTime = sendTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getComputeTime() {
|
||||||
|
return computeTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComputeTime(long computeTime) {
|
||||||
|
this.computeTime = computeTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getReceiveTime() {
|
||||||
|
return receiveTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReceiveTime(long receiveTime) {
|
||||||
|
this.receiveTime = receiveTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSendCount() {
|
||||||
|
return sendCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSendCount(int sendCount) {
|
||||||
|
this.sendCount = sendCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getReceiveCount() {
|
||||||
|
return receiveCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReceiveCount(int receiveCount) {
|
||||||
|
this.receiveCount = receiveCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取总耗时
|
||||||
|
*/
|
||||||
|
public long getTotalTime() {
|
||||||
|
return sendTime + computeTime + receiveTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取各阶段耗时占比
|
||||||
|
*/
|
||||||
|
public double getSendPercentage() {
|
||||||
|
long total = getTotalTime();
|
||||||
|
return total == 0 ? 0 : (double) sendTime / total * 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getComputePercentage() {
|
||||||
|
long total = getTotalTime();
|
||||||
|
return total == 0 ? 0 : (double) computeTime / total * 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getReceivePercentage() {
|
||||||
|
long total = getTotalTime();
|
||||||
|
return total == 0 ? 0 : (double) receiveTime / total * 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format(
|
||||||
|
"TimeStats{sendTime=%dms(%.1f%%), computeTime=%dms(%.1f%%), receiveTime=%dms(%.1f%%), total=%dms, sendCount=%d, receiveCount=%d}",
|
||||||
|
sendTime, getSendPercentage(),
|
||||||
|
computeTime, getComputePercentage(),
|
||||||
|
receiveTime, getReceivePercentage(),
|
||||||
|
getTotalTime(),
|
||||||
|
sendCount, receiveCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,174 @@
|
|||||||
|
package com.sunyard.util;
|
||||||
|
|
||||||
|
import java.nio.ByteOrder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字节数组转换工具类,提供常见的基本类型与字节数组之间的互转功能。
|
||||||
|
* 默认采用大端字节序(高位在前),也支持通过参数指定字节序。
|
||||||
|
*
|
||||||
|
* @author YourName
|
||||||
|
*/
|
||||||
|
public final class BytesUtil {
|
||||||
|
|
||||||
|
private BytesUtil() {
|
||||||
|
// 私有构造方法,防止实例化
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 无符号 short 转字节数组 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将无符号 short 值(以 int 形式传入,范围 0 ~ 65535)转换为大端字节序的字节数组(2字节)。
|
||||||
|
*
|
||||||
|
* @param value 无符号 short 值(0 到 65535)
|
||||||
|
* @return 长度为 2 的字节数组,高位在前
|
||||||
|
* @throws IllegalArgumentException 如果 value 超出 0~65535 范围
|
||||||
|
*/
|
||||||
|
public static byte[] unsignedShortToBytes(int value) {
|
||||||
|
if (value < 0 || value > 0xFFFF) {
|
||||||
|
throw new IllegalArgumentException("Value out of range for unsigned short: " + value);
|
||||||
|
}
|
||||||
|
return new byte[]{
|
||||||
|
(byte) ((value >> 8) & 0xFF),
|
||||||
|
(byte) (value & 0xFF)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将无符号 short 值(以 short 类型传入,自动按无符号处理)转换为大端字节序的字节数组(2字节)。
|
||||||
|
* 例如,传入 (short) 0x8001 会得到 { (byte)0x80, (byte)0x01 }。
|
||||||
|
*
|
||||||
|
* @param value short 类型,将按其二进制补码表示直接作为无符号值处理
|
||||||
|
* @return 长度为 2 的字节数组,高位在前
|
||||||
|
*/
|
||||||
|
public static byte[] unsignedShortToBytes(short value) {
|
||||||
|
// 将 short 转为无符号 int
|
||||||
|
int unsignedValue = value & 0xFFFF;
|
||||||
|
return unsignedShortToBytes(unsignedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将无符号 short 值(int 类型)转换为指定字节序的字节数组(2字节)。
|
||||||
|
*
|
||||||
|
* @param value 无符号 short 值(0 到 65535)
|
||||||
|
* @param byteOrder 字节序,ByteOrder.BIG_ENDIAN 或 ByteOrder.LITTLE_ENDIAN
|
||||||
|
* @return 长度为 2 的字节数组
|
||||||
|
* @throws IllegalArgumentException 如果 value 超出范围或 byteOrder 为 null
|
||||||
|
*/
|
||||||
|
public static byte[] unsignedShortToBytes(int value, ByteOrder byteOrder) {
|
||||||
|
if (byteOrder == null) {
|
||||||
|
throw new IllegalArgumentException("ByteOrder must not be null");
|
||||||
|
}
|
||||||
|
byte[] bytes = unsignedShortToBytes(value);
|
||||||
|
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||||
|
// 交换高低位
|
||||||
|
byte tmp = bytes[0];
|
||||||
|
bytes[0] = bytes[1];
|
||||||
|
bytes[1] = tmp;
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将无符号 short 值(short 类型)转换为指定字节序的字节数组(2字节)。
|
||||||
|
*
|
||||||
|
* @param value short 类型,按无符号处理
|
||||||
|
* @param byteOrder 字节序
|
||||||
|
* @return 长度为 2 的字节数组
|
||||||
|
*/
|
||||||
|
public static byte[] unsignedShortToBytes(short value, ByteOrder byteOrder) {
|
||||||
|
return unsignedShortToBytes(value & 0xFFFF, byteOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 字节数组转无符号 short ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从字节数组的起始位置读取 2 字节,按大端序转换为无符号 short 值(返回 int 类型)。
|
||||||
|
*
|
||||||
|
* @param bytes 字节数组
|
||||||
|
* @param offset 起始偏移量
|
||||||
|
* @return 无符号 short 值(0 ~ 65535)
|
||||||
|
* @throws IllegalArgumentException 如果 bytes 为 null 或长度不足
|
||||||
|
*/
|
||||||
|
public static int bytesToUnsignedShort(byte[] bytes, int offset) {
|
||||||
|
if (bytes == null || offset + 2 > bytes.length) {
|
||||||
|
throw new IllegalArgumentException("Invalid byte array or offset");
|
||||||
|
}
|
||||||
|
return ((bytes[offset] & 0xFF) << 8) | (bytes[offset + 1] & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从字节数组的起始位置读取 2 字节,按指定字节序转换为无符号 short 值。
|
||||||
|
*
|
||||||
|
* @param bytes 字节数组
|
||||||
|
* @param offset 起始偏移量
|
||||||
|
* @param byteOrder 字节序
|
||||||
|
* @return 无符号 short 值
|
||||||
|
*/
|
||||||
|
public static int bytesToUnsignedShort(byte[] bytes, int offset, ByteOrder byteOrder) {
|
||||||
|
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||||
|
return ((bytes[offset + 1] & 0xFF) << 8) | (bytes[offset] & 0xFF);
|
||||||
|
} else {
|
||||||
|
return bytesToUnsignedShort(bytes, offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 其他常见转换(扩展) ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 int 值转换为大端序的 4 字节数组。
|
||||||
|
*/
|
||||||
|
public static byte[] intToBytes(int value) {
|
||||||
|
return new byte[]{
|
||||||
|
(byte) ((value >> 24) & 0xFF),
|
||||||
|
(byte) ((value >> 16) & 0xFF),
|
||||||
|
(byte) ((value >> 8) & 0xFF),
|
||||||
|
(byte) (value & 0xFF)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将大端序的 4 字节数组转换为 int 值。
|
||||||
|
*/
|
||||||
|
public static int bytesToInt(byte[] bytes, int offset) {
|
||||||
|
if (bytes == null || offset + 4 > bytes.length) {
|
||||||
|
throw new IllegalArgumentException("Invalid byte array or offset");
|
||||||
|
}
|
||||||
|
return ((bytes[offset] & 0xFF) << 24)
|
||||||
|
| ((bytes[offset + 1] & 0xFF) << 16)
|
||||||
|
| ((bytes[offset + 2] & 0xFF) << 8)
|
||||||
|
| (bytes[offset + 3] & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将 long 值转换为大端序的 8 字节数组。
|
||||||
|
*/
|
||||||
|
public static byte[] longToBytes(long value) {
|
||||||
|
return new byte[]{
|
||||||
|
(byte) ((value >> 56) & 0xFF),
|
||||||
|
(byte) ((value >> 48) & 0xFF),
|
||||||
|
(byte) ((value >> 40) & 0xFF),
|
||||||
|
(byte) ((value >> 32) & 0xFF),
|
||||||
|
(byte) ((value >> 24) & 0xFF),
|
||||||
|
(byte) ((value >> 16) & 0xFF),
|
||||||
|
(byte) ((value >> 8) & 0xFF),
|
||||||
|
(byte) (value & 0xFF)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将大端序的 8 字节数组转换为 long 值。
|
||||||
|
*/
|
||||||
|
public static long bytesToLong(byte[] bytes, int offset) {
|
||||||
|
if (bytes == null || offset + 8 > bytes.length) {
|
||||||
|
throw new IllegalArgumentException("Invalid byte array or offset");
|
||||||
|
}
|
||||||
|
return ((long) (bytes[offset] & 0xFF) << 56)
|
||||||
|
| ((long) (bytes[offset + 1] & 0xFF) << 48)
|
||||||
|
| ((long) (bytes[offset + 2] & 0xFF) << 40)
|
||||||
|
| ((long) (bytes[offset + 3] & 0xFF) << 32)
|
||||||
|
| ((long) (bytes[offset + 4] & 0xFF) << 24)
|
||||||
|
| ((long) (bytes[offset + 5] & 0xFF) << 16)
|
||||||
|
| ((long) (bytes[offset + 6] & 0xFF) << 8)
|
||||||
|
| ((long) (bytes[offset + 7] & 0xFF));
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,28 +1,49 @@
|
|||||||
package cmbpoc;
|
package cmbpoc;
|
||||||
|
|
||||||
|
import com.sunyard.RetWrap;
|
||||||
|
import com.sunyard.SydApi;
|
||||||
|
import com.sunyard.proto.PacketSection;
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
import org.junit.After;
|
import org.junit.*;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
public class FunctionTest {
|
public class FunctionTest {
|
||||||
|
|
||||||
private SydApi4j api;
|
private SydApi4j api;
|
||||||
private String keypairIndex;
|
private String keypairIndex4Sign = "0000000000000sge00000000001";
|
||||||
private byte[] privateKey;
|
private String keypairIndex4Ende = "0000000000000sge00100000001";
|
||||||
private byte[] publicKey;
|
private byte[] privateKey4Sign;
|
||||||
private byte[] orgData;
|
private byte[] publicKey4Sign;
|
||||||
|
private byte[] privateKey4Ende;
|
||||||
|
private byte[] publicKey4Ende;
|
||||||
|
private byte[] orgData = new byte[1024];
|
||||||
|
private byte[] orgData2K = new byte[ 2 * 1024 ];
|
||||||
|
private byte[] orgData2M = new byte[ 2 * 1024 * 1024 ];
|
||||||
|
private byte[] orgData10M = new byte[ 10 * 1024 * 1024 ];
|
||||||
private String sign;
|
private String sign;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void start(){
|
public void start(){
|
||||||
// 调试模式
|
// 调试模式
|
||||||
|
|
||||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
// 建立链接(单台)
|
// 建立链接(单台)
|
||||||
this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000);
|
this.api = (SydApi4j) new SydApi4j().connect("192.168.100.145", 8889, null, 30000);
|
||||||
|
|
||||||
|
// RetWrap keypair = this.getPrivateKeyAndPublickKey(keypairIndex4Sign);
|
||||||
|
// this.publicKey4Sign = Util.hexString2Bytes( keypair.get("pk").toString() );
|
||||||
|
// this.privateKey4Sign = Util.hexString2Bytes( keypair.get("sk").toString() );
|
||||||
|
//
|
||||||
|
// RetWrap keypair4Ende = this.getPrivateKeyAndPublickKey(keypairIndex4Sign);
|
||||||
|
// this.publicKey4Ende = Util.hexString2Bytes( keypair4Ende.get("pk").toString() );
|
||||||
|
// this.privateKey4Ende = Util.hexString2Bytes( keypair4Ende.get("sk").toString() );
|
||||||
|
|
||||||
|
// 使用预置密钥对
|
||||||
|
this.publicKey4Sign = Util.hexString2Bytes( "034200046508BC4FE965BEB1D57715C180135FC861FDC6527E82E7FD7A6CA25E7CE9CE75101181D7E2CDD16C1974652B38A045465C7FFBAC700E49F949AE5F4C57AEBEAA" );
|
||||||
|
this.privateKey4Sign = Util.hexString2Bytes( "00010000CF945DAFF49B9F3AB67A23E1E1BA700FF1F78F5DFFCBB518FD1303A51DABA6FC0000000000000000000000000000000000000000000000000000000000000000" );
|
||||||
|
this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7");
|
||||||
|
this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -36,37 +57,99 @@ public class FunctionTest {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Ignore
|
||||||
@Test
|
@Test
|
||||||
public void getPrivateKeyAndPublickKey(){
|
public void genKeyPair(){
|
||||||
this.publicKey = Util.hexString2Bytes(
|
// RetWrap r = this.api.SYD_SM2GenKeyPair();
|
||||||
this.api.SM2GetPublicKeyC(keypairIndex)
|
// String pk = r.get("publicKey").toString();
|
||||||
);
|
// String sk = r.get("privateKey").toString();
|
||||||
|
//
|
||||||
|
// System.out.println("pk=" + pk);
|
||||||
|
// System.out.println("sk=" + sk);
|
||||||
|
//
|
||||||
|
// this.privateKey4Sign = Util.hexString2Bytes(sk);
|
||||||
|
// this.publicKey4Sign = Util.hexString2Bytes(pk);
|
||||||
|
|
||||||
|
RetWrap rw = this.api.SM2GenKeyPair();
|
||||||
|
byte[] pk = (byte[]) rw.get(SydApi.RET_PUBLIC_KEY);
|
||||||
|
byte[] sk = (byte[]) rw.get(SydApi.RET_PRIVATE_KEY);
|
||||||
|
System.out.println("pk=" + Util.bytes2HexString(pk));
|
||||||
|
System.out.println("sk=" + Util.bytes2HexString(sk));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
this.privateKey = Util.hexString2Bytes(
|
public RetWrap getPrivateKeyAndPublickKey(String keypairIndex){
|
||||||
this.api.SM2GetPrivateKeyC(keypairIndex)
|
String pk = this.api.SM2GetPublicKeyC(keypairIndex);
|
||||||
);
|
String sk = this.api.SM2GetPrivateKeyC(keypairIndex);
|
||||||
|
|
||||||
|
System.out.println("pk=" + pk);
|
||||||
|
System.out.println("sk=" + sk);
|
||||||
|
|
||||||
|
RetWrap retWrap = new RetWrap();
|
||||||
|
retWrap.put("pk", pk);
|
||||||
|
retWrap.put("sk", sk);
|
||||||
|
return retWrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SYD_SM2_Sign(){
|
public void SYD_SM2_Sign(){
|
||||||
this.api.SYD_SM2_Sign_HA(1, privateKey, publicKey, orgData);
|
String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData);
|
||||||
|
this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData, sign);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SYD_SM2_Verify(){
|
public void SYD_SM2_Sign_2M(){
|
||||||
this.api.SYD_SM2_Verify_HA(1, publicKey, orgData, sign);
|
String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData2M);
|
||||||
|
this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData2M, sign);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Sign_10M(){
|
||||||
|
String sign = this.api.SYD_SM2_Sign_HA( SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, orgData10M);
|
||||||
|
this.api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, orgData10M, sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void SYD_SM2_Encrypt(){
|
public void SYD_SM2_Encrypt(){
|
||||||
this.api.SM2Decrypt();
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData);
|
||||||
|
System.out.println("密文=" + Util.bytes2HexString(enData));
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
Assert.assertArrayEquals(orgData, deData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_2K(){
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2K);
|
||||||
|
System.out.println("密文=" + Util.bytes2HexString(enData));
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
Assert.assertArrayEquals(orgData2K, deData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_2M(){
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M);
|
||||||
|
System.out.println("密文长度=" + enData.length);
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
Assert.assertArrayEquals(orgData2M, deData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_10M(){
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M);
|
||||||
|
System.out.println("密文=" + Util.bytes2HexString(enData));
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
Assert.assertArrayEquals(orgData10M, deData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
package cmbpoc;
|
||||||
|
|
||||||
|
|
||||||
|
import com.sunyard.SydApi;
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import org.apache.jmeter.config.Arguments;
|
||||||
|
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
|
||||||
|
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
|
||||||
|
import org.apache.jmeter.samplers.SampleResult;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
|
||||||
|
public class JmeterTest extends AbstractJavaSamplerClient {
|
||||||
|
|
||||||
|
private byte[] publicKey;
|
||||||
|
private byte[] plainData;
|
||||||
|
private byte[] encryptedData;
|
||||||
|
private String sign;
|
||||||
|
private int sleep = 1;
|
||||||
|
|
||||||
|
private byte[] privateKey4Sign;
|
||||||
|
private byte[] publicKey4Sign;
|
||||||
|
private byte[] privateKey4Ende;
|
||||||
|
private byte[] publicKey4Ende;
|
||||||
|
|
||||||
|
private String ip ;
|
||||||
|
private int port;
|
||||||
|
private int timeout;
|
||||||
|
private String ip2 ;
|
||||||
|
|
||||||
|
private ThreadLocal<SydApi4j> api;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Arguments getDefaultParameters() {
|
||||||
|
Arguments params = new Arguments();
|
||||||
|
params.addArgument("function", "sleep");
|
||||||
|
params.addArgument("sleep", "1");
|
||||||
|
params.addArgument("plainDataLen", "1024");
|
||||||
|
params.addArgument("publicKey4Ende", "", "加密用的公钥");
|
||||||
|
params.addArgument("privateKey4Ende", "", "解密用的公钥");
|
||||||
|
params.addArgument("publicKey4Sign", "", "验签用的公钥");
|
||||||
|
params.addArgument("privateKey4Sign", "", "签名用的公钥");
|
||||||
|
params.addArgument("ip", "192.168.100.145");
|
||||||
|
params.addArgument("ip2", "172.1.41.96");
|
||||||
|
params.addArgument("port", "8889");
|
||||||
|
params.addArgument("timeout", "3000");
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setupTest(JavaSamplerContext context) {
|
||||||
|
// 签名密钥对
|
||||||
|
String publicKey4Sign = context.getParameter("publicKey4Sign");
|
||||||
|
if ( null != publicKey4Sign && !publicKey4Sign.isEmpty()) {
|
||||||
|
this.publicKey4Sign = Util.hexString2Bytes(publicKey4Sign);
|
||||||
|
}
|
||||||
|
String privateKey4Sign = context.getParameter("privateKey4Sign");
|
||||||
|
if ( null != privateKey4Sign && !privateKey4Sign.isEmpty()) {
|
||||||
|
this.privateKey4Sign = Util.hexString2Bytes(privateKey4Sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加密密钥对
|
||||||
|
String publicKey4Ende = context.getParameter("publicKey4Ende");
|
||||||
|
if ( null != publicKey4Ende && !publicKey4Ende.isEmpty()) {
|
||||||
|
this.publicKey4Ende = Util.hexString2Bytes(publicKey4Ende);
|
||||||
|
}
|
||||||
|
String privateKey4Ende = context.getParameter("privateKey4Ende");
|
||||||
|
if ( null != privateKey4Ende && !privateKey4Ende.isEmpty()) {
|
||||||
|
this.privateKey4Ende = Util.hexString2Bytes(privateKey4Ende);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 原始数据
|
||||||
|
int plainDataLen = context.getIntParameter("plainDataLen");
|
||||||
|
plainData = new byte[plainDataLen];
|
||||||
|
new Random().nextBytes(plainData);
|
||||||
|
System.out.println("原始数据长度:" + plainDataLen);
|
||||||
|
|
||||||
|
// 校准测试
|
||||||
|
sleep = context.getIntParameter("sleep", 1);
|
||||||
|
|
||||||
|
// api
|
||||||
|
ip = context.getParameter("ip");
|
||||||
|
ip2 = context.getParameter("ip2");
|
||||||
|
port = context.getIntParameter("port");
|
||||||
|
timeout = context.getIntParameter("timeout");
|
||||||
|
api = new ThreadLocal<SydApi4j>() {
|
||||||
|
@Override
|
||||||
|
public SydApi4j initialValue() {
|
||||||
|
return (SydApi4j) new SydApi4j().connect(ip, port, null, timeout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 线程开始时准备解密、验签数据
|
||||||
|
String function = context.getParameter("function");
|
||||||
|
switch (function) {
|
||||||
|
case "SYD_SM2_Decrypt":
|
||||||
|
this.encryptedData = this.api.get().SYD_SM2Encrypt(this.publicKey4Ende, this.plainData);
|
||||||
|
break;
|
||||||
|
case "SYD_SM2_Verify":
|
||||||
|
this.sign = this.api.get().SYD_SM2_Sign_HA(SydApi.DATA_HASH, this.privateKey4Sign, this.publicKey4Sign, this.plainData);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SampleResult runTest(JavaSamplerContext context) {
|
||||||
|
SampleResult result = new SampleResult();
|
||||||
|
// 调用待测函数
|
||||||
|
String function = context.getParameter("function");
|
||||||
|
|
||||||
|
result.sampleStart(); // 开始计时
|
||||||
|
try {
|
||||||
|
switch (function) {
|
||||||
|
case "SYD_SM2_Encrypt":
|
||||||
|
this.api.get().SYD_SM2Encrypt(publicKey4Ende, this.plainData);
|
||||||
|
break;
|
||||||
|
case "SYD_SM2_Decrypt":
|
||||||
|
this.api.get().SYD_SM2Decrypt(privateKey4Ende, this.encryptedData);
|
||||||
|
break;
|
||||||
|
case "SYD_SM2_Sign":
|
||||||
|
this.api.get().SYD_SM2_Sign_HA(SydApi.DATA_HASH, privateKey4Sign, publicKey4Sign, this.plainData);
|
||||||
|
break;
|
||||||
|
case "SYD_SM2_Verify":
|
||||||
|
this.api.get().SYD_SM2_Verify_HA(SydApi.DATA_HASH, publicKey4Sign, this.plainData, this.sign);
|
||||||
|
break;
|
||||||
|
case "sleep":
|
||||||
|
Thread.sleep( this.sleep );
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// result.setResponseData(encrypted); // 可选
|
||||||
|
result.setSuccessful(true);
|
||||||
|
result.setResponseCodeOK();
|
||||||
|
result.setResponseMessage( function + "执行成功" );
|
||||||
|
} catch (Exception e) {
|
||||||
|
result.setSuccessful(false);
|
||||||
|
result.setResponseCode("500");
|
||||||
|
result.setResponseMessage( function + "执行失败。" + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
result.sampleEnd(); // 结束计时
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void teardownTest(JavaSamplerContext context) {
|
||||||
|
// 释放资源(如有)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package cmbpoc;
|
||||||
|
|
||||||
|
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.jmeter.config.Arguments;
|
||||||
|
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
|
||||||
|
import org.apache.jmeter.samplers.SampleResult;
|
||||||
|
|
||||||
|
public class JmeterTestTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 1. 创建 JMeter 参数集,并填入您提供的所有参数
|
||||||
|
Arguments jmeterArgs = new Arguments();
|
||||||
|
jmeterArgs.addArgument("function", "SYD_SM2_Encrypt");
|
||||||
|
jmeterArgs.addArgument("sleep", "1");
|
||||||
|
jmeterArgs.addArgument("plainDataLen", "2048");
|
||||||
|
jmeterArgs.addArgument("publicKey4Ende", "03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7");
|
||||||
|
jmeterArgs.addArgument("privateKey4Ende", "00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
|
jmeterArgs.addArgument("ip", "192.168.100.145");
|
||||||
|
jmeterArgs.addArgument("ip2", "172.1.41.96");
|
||||||
|
jmeterArgs.addArgument("port", "8889");
|
||||||
|
jmeterArgs.addArgument("timeout", "3000");
|
||||||
|
|
||||||
|
// 2. 基于参数集创建 JMeter 上下文对象
|
||||||
|
JavaSamplerContext context = new JavaSamplerContext(jmeterArgs);
|
||||||
|
|
||||||
|
// 3. 创建您的测试类实例
|
||||||
|
JmeterTest jmeterTest = new JmeterTest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 4. 模拟 JMeter 执行周期:setup -> run -> teardown
|
||||||
|
jmeterTest.setupTest(context);
|
||||||
|
|
||||||
|
SampleResult result = jmeterTest.runTest(context);
|
||||||
|
if (result != null) {
|
||||||
|
System.out.println("SampleResult 状态: " + result.getResponseCode());
|
||||||
|
System.out.println("响应数据: " + result.getResponseDataAsString());
|
||||||
|
System.out.println("是否成功: " + result.isSuccessful());
|
||||||
|
} else {
|
||||||
|
System.out.println("runTest 返回 null,请检查实现逻辑");
|
||||||
|
}
|
||||||
|
|
||||||
|
jmeterTest.teardownTest(context);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("执行测试时发生异常:");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
package cmbpoc;
|
||||||
|
|
||||||
|
import com.sunyard.RetWrap;
|
||||||
|
import com.sunyard.SydApi;
|
||||||
|
import com.sunyard.entity.TimeStats;
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import org.junit.*;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
public class PerformanceTest {
|
||||||
|
|
||||||
|
private SydApi4j api;
|
||||||
|
private byte[] privateKey4Ende;
|
||||||
|
private byte[] publicKey4Ende;
|
||||||
|
private byte[] orgData2K = new byte[2 * 1024];
|
||||||
|
private byte[] orgData2M = new byte[2 * 1024 * 1024];
|
||||||
|
private byte[] orgData10M = new byte[10 * 1024 * 1024];
|
||||||
|
private static final int TEST_ROUNDS = 10;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void start() {
|
||||||
|
System.setProperty("com.sunyard.sydapi4j.debug", "false");
|
||||||
|
String ip = System.getProperty("ip", "192.168.100.145");
|
||||||
|
System.out.println("ip=" + ip);
|
||||||
|
this.api = (SydApi4j) new SydApi4j().connect(ip, 8889, null, 30000);
|
||||||
|
this.publicKey4Ende = Util.hexString2Bytes("03420004AF3D4E55D26564C6C937E6EA9232691C01AC5E7916AD2136F788CE7C1E4D0AE8C4754DEC2F7F6A78962D51A171A6F5B4F11823390AEC8B5B0246CD2CCF8052B7");
|
||||||
|
this.privateKey4Ende = Util.hexString2Bytes("00010000963F7C3A98B7810F52D2861A060E18A9D4D59796CF33967DD61EEDA091E1AD9B0000000000000000000000000000000000000000000000000000000000000000");
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void stop() {
|
||||||
|
if (null != api) {
|
||||||
|
this.api.disconnect();
|
||||||
|
this.api = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_Performance_2K() {
|
||||||
|
System.out.println("=== SYD_SM2_Encrypt Performance Test - 2K Data ===");
|
||||||
|
long totalEncryptTime = 0;
|
||||||
|
long totalDecryptTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < TEST_ROUNDS; i++) {
|
||||||
|
long encryptStart = System.currentTimeMillis();
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2K);
|
||||||
|
long encryptEnd = System.currentTimeMillis();
|
||||||
|
|
||||||
|
long decryptStart = System.currentTimeMillis();
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
long decryptEnd = System.currentTimeMillis();
|
||||||
|
|
||||||
|
totalEncryptTime += (encryptEnd - encryptStart);
|
||||||
|
totalDecryptTime += (decryptEnd - decryptStart);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(orgData2K, deData);
|
||||||
|
}
|
||||||
|
|
||||||
|
double avgEncryptTime = (double) totalEncryptTime / TEST_ROUNDS;
|
||||||
|
double avgDecryptTime = (double) totalDecryptTime / TEST_ROUNDS;
|
||||||
|
double encryptThroughput = (2.0 / avgEncryptTime) * 1000;
|
||||||
|
double decryptThroughput = (2.0 / avgDecryptTime) * 1000;
|
||||||
|
|
||||||
|
System.out.println("Test Rounds: " + TEST_ROUNDS);
|
||||||
|
System.out.println("Data Size: 2KB");
|
||||||
|
System.out.printf("Average Encrypt Time: %.2f ms%n", avgEncryptTime);
|
||||||
|
System.out.printf("Average Decrypt Time: %.2f ms%n", avgDecryptTime);
|
||||||
|
System.out.printf("Encrypt Throughput: %.2f KB/s%n", encryptThroughput);
|
||||||
|
System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_Performance_2M() {
|
||||||
|
System.out.println("=== SYD_SM2_Encrypt Performance Test - 2M Data ===");
|
||||||
|
long totalEncryptTime = 0;
|
||||||
|
long totalDecryptTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < TEST_ROUNDS; i++) {
|
||||||
|
long encryptStart = System.currentTimeMillis();
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M);
|
||||||
|
long encryptEnd = System.currentTimeMillis();
|
||||||
|
|
||||||
|
long decryptStart = System.currentTimeMillis();
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
long decryptEnd = System.currentTimeMillis();
|
||||||
|
|
||||||
|
totalEncryptTime += (encryptEnd - encryptStart);
|
||||||
|
totalDecryptTime += (decryptEnd - decryptStart);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(orgData2M, deData);
|
||||||
|
}
|
||||||
|
|
||||||
|
double avgEncryptTime = (double) totalEncryptTime / TEST_ROUNDS;
|
||||||
|
double avgDecryptTime = (double) totalDecryptTime / TEST_ROUNDS;
|
||||||
|
double encryptThroughput = (2048.0 / avgEncryptTime) * 1000;
|
||||||
|
double decryptThroughput = (2048.0 / avgDecryptTime) * 1000;
|
||||||
|
|
||||||
|
System.out.println("Test Rounds: " + TEST_ROUNDS);
|
||||||
|
System.out.println("Data Size: 2MB");
|
||||||
|
System.out.printf("Average Encrypt Time: %.2f ms%n", avgEncryptTime);
|
||||||
|
System.out.printf("Average Decrypt Time: %.2f ms%n", avgDecryptTime);
|
||||||
|
System.out.printf("Encrypt Throughput: %.2f KB/s%n", encryptThroughput);
|
||||||
|
System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_Performance_10M() {
|
||||||
|
System.out.println("=== SYD_SM2_Encrypt Performance Test - 10M Data ===");
|
||||||
|
long totalEncryptTime = 0;
|
||||||
|
long totalDecryptTime = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < TEST_ROUNDS; i++) {
|
||||||
|
long encryptStart = System.currentTimeMillis();
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData10M);
|
||||||
|
long encryptEnd = System.currentTimeMillis();
|
||||||
|
|
||||||
|
long decryptStart = System.currentTimeMillis();
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
long decryptEnd = System.currentTimeMillis();
|
||||||
|
|
||||||
|
totalEncryptTime += (encryptEnd - encryptStart);
|
||||||
|
totalDecryptTime += (decryptEnd - decryptStart);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(orgData10M, deData);
|
||||||
|
}
|
||||||
|
|
||||||
|
double avgEncryptTime = (double) totalEncryptTime / TEST_ROUNDS;
|
||||||
|
double avgDecryptTime = (double) totalDecryptTime / TEST_ROUNDS;
|
||||||
|
double encryptThroughput = (10240.0 / avgEncryptTime) * 1000;
|
||||||
|
double decryptThroughput = (10240.0 / avgDecryptTime) * 1000;
|
||||||
|
|
||||||
|
System.out.println("Test Rounds: " + TEST_ROUNDS);
|
||||||
|
System.out.println("Data Size: 10MB");
|
||||||
|
System.out.printf("Average Encrypt Time: %.2f ms%n", avgEncryptTime);
|
||||||
|
System.out.printf("Average Decrypt Time: %.2f ms%n", avgDecryptTime);
|
||||||
|
System.out.printf("Encrypt Throughput: %.2f KB/s%n", encryptThroughput);
|
||||||
|
System.out.printf("Decrypt Throughput: %.2f KB/s%n", decryptThroughput);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt_TimeStats_2M() {
|
||||||
|
System.out.println("=== SYD_SM2_Encrypt Time Statistics Test - 2M Data ===");
|
||||||
|
|
||||||
|
// 预热
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M);
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 正式测试
|
||||||
|
TimeStats encryptStats = new TimeStats();
|
||||||
|
TimeStats decryptStats = new TimeStats();
|
||||||
|
|
||||||
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey4Ende, orgData2M, encryptStats);
|
||||||
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey4Ende, enData, decryptStats);
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(orgData2M, deData);
|
||||||
|
|
||||||
|
System.out.println("\n--- 加密阶段时间统计 ---");
|
||||||
|
System.out.printf("发送阶段: %d ms (%.1f%%), 发送次数: %d%n",
|
||||||
|
encryptStats.getSendTime(), encryptStats.getSendPercentage(), encryptStats.getSendCount());
|
||||||
|
System.out.printf("计算阶段: %d ms (%.1f%%)%n",
|
||||||
|
encryptStats.getComputeTime(), encryptStats.getComputePercentage());
|
||||||
|
System.out.printf("接收阶段: %d ms (%.1f%%), 接收次数: %d%n",
|
||||||
|
encryptStats.getReceiveTime(), encryptStats.getReceivePercentage(), encryptStats.getReceiveCount());
|
||||||
|
System.out.printf("加密总耗时: %d ms%n", encryptStats.getTotalTime());
|
||||||
|
|
||||||
|
System.out.println("\n--- 解密阶段时间统计 ---");
|
||||||
|
System.out.printf("发送阶段: %d ms (%.1f%%), 发送次数: %d%n",
|
||||||
|
decryptStats.getSendTime(), decryptStats.getSendPercentage(), decryptStats.getSendCount());
|
||||||
|
System.out.printf("计算阶段: %d ms (%.1f%%)%n",
|
||||||
|
decryptStats.getComputeTime(), decryptStats.getComputePercentage());
|
||||||
|
System.out.printf("接收阶段: %d ms (%.1f%%), 接收次数: %d%n",
|
||||||
|
decryptStats.getReceiveTime(), decryptStats.getReceivePercentage(), decryptStats.getReceiveCount());
|
||||||
|
System.out.printf("解密总耗时: %d ms%n", decryptStats.getTotalTime());
|
||||||
|
|
||||||
|
System.out.println("\n--- 汇总 ---");
|
||||||
|
long totalTime = encryptStats.getTotalTime() + decryptStats.getTotalTime();
|
||||||
|
System.out.printf("加密解密总耗时: %d ms%n", totalTime);
|
||||||
|
System.out.printf("加密吞吐量: %.2f KB/s%n", (2048.0 / encryptStats.getTotalTime()) * 1000);
|
||||||
|
System.out.printf("解密吞吐量: %.2f KB/s%n", (2048.0 / decryptStats.getTotalTime()) * 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package cmbpoc;
|
||||||
|
|
||||||
|
import com.sunyard.RetWrap;
|
||||||
|
import com.sunyard.SydApi;
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
public class SignatureVerifyTest {
|
||||||
|
|
||||||
|
private SydApi4j api;
|
||||||
|
private byte[] orgData;
|
||||||
|
private String SM2dn;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
this.api = (SydApi4j) new SydApi4j().connect("172.1.41.139", 8889, null, 3000);
|
||||||
|
this.orgData = "Hello, World! 测试数据".getBytes();
|
||||||
|
this.SM2dn = "C=cn,O=CFCA OCA1 SM2,OU=Local RA OCA1,OU=Units,CN=041@N91330108762038374D@sunyard@00000064";
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
if (api != null) {
|
||||||
|
this.api.disconnect();
|
||||||
|
this.api = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testP1SignAndVerify() {
|
||||||
|
byte[] sign = this.api.SYD_NakedSign(orgData, SM2dn);
|
||||||
|
Assert.assertNotNull("P1签名结果不应为空", sign);
|
||||||
|
System.out.println("P1签名结果: " + Util.bytes2HexString(sign));
|
||||||
|
|
||||||
|
boolean verifyResult = this.api.SYD_NakedVerify(orgData, sign, SM2dn);
|
||||||
|
Assert.assertTrue("P1验签应通过", verifyResult);
|
||||||
|
System.out.println("P1验签结果: " + verifyResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testP7DetachSignAndVerify() {
|
||||||
|
String sign = this.api.detachedSign(1, orgData, SM2dn);
|
||||||
|
Assert.assertNotNull("P7 Detach签名结果不应为空", sign);
|
||||||
|
System.out.println("P7 Detach签名结果: " + sign);
|
||||||
|
|
||||||
|
boolean verifyResult = this.api.detachedVerify(1, orgData, sign);
|
||||||
|
Assert.assertTrue("P7 Detach验签应通过", verifyResult);
|
||||||
|
System.out.println("P7 Detach验签结果: " + verifyResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testP7AttachSignAndVerify() throws Exception {
|
||||||
|
String sign = this.api.attachedSign(orgData, SM2dn);
|
||||||
|
Assert.assertNotNull("P7 Attach签名结果不应为空", sign);
|
||||||
|
System.out.println("P7 Attach签名结果: " + sign);
|
||||||
|
|
||||||
|
boolean verifyResult = this.api.attachedVerify(sign);
|
||||||
|
Assert.assertTrue("P7 Attach验签应通过", verifyResult);
|
||||||
|
System.out.println("P7 Attach验签结果: " + verifyResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDigitalEnvelopeEncryptAndDecrypt() {
|
||||||
|
RetWrap genResult = this.api.generateDEByDn(SM2dn, orgData);
|
||||||
|
Assert.assertNotNull("数字信封生成结果不应为空", genResult);
|
||||||
|
|
||||||
|
byte[] cipherKey = (byte[]) genResult.get(SydApi.RET_CIPHER_KEY);
|
||||||
|
Assert.assertNotNull("数字信封密文密钥不应为空", cipherKey);
|
||||||
|
System.out.println("数字信封密文: " + new String(cipherKey));
|
||||||
|
|
||||||
|
RetWrap decryptResult = this.api.decryptDEByDN(SM2dn, cipherKey);
|
||||||
|
Assert.assertNotNull("数字信封解密结果不应为空", decryptResult);
|
||||||
|
|
||||||
|
byte[] decryptedData = (byte[]) decryptResult.get("oData");
|
||||||
|
Assert.assertNotNull("解密数据不应为空", decryptedData);
|
||||||
|
Assert.assertArrayEquals("解密数据应与原始数据一致", orgData, decryptedData);
|
||||||
|
System.out.println("数字信封解密结果: " + new String(decryptedData));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package com.sunyard.sydapi.loadrunner;
|
|
||||||
|
|
||||||
import com.sunyard.sydapi.loadrunner.action1.Actions;
|
|
||||||
import racal.sunyard.main.SydApi4j;
|
|
||||||
import racal.sunyard.main.SydApiBuilder;
|
|
||||||
|
|
||||||
public class ShangTest {
|
|
||||||
|
|
||||||
private static final String dn = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
|
||||||
private static final byte[] testData = new byte[ 4 * 1024 ];
|
|
||||||
|
|
||||||
private SydApi4j api = null;
|
|
||||||
|
|
||||||
public void init() throws Exception {
|
|
||||||
if ( null == api ) {
|
|
||||||
api = (SydApi4j) new SydApiBuilder()
|
|
||||||
.setIp("172.16.18.59") // 192.168.0.162
|
|
||||||
.setPort( 8889 )
|
|
||||||
.useDebugMode()
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void action(){
|
|
||||||
String sign = api.attachedSign(testData, dn);
|
|
||||||
boolean ret = api.attachedVerify( sign );
|
|
||||||
|
|
||||||
assert ret;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void end(){
|
|
||||||
if (null != this.api ) {
|
|
||||||
this.api.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Throwable {
|
|
||||||
System.out.println( SydApi4j.getVersion() );
|
|
||||||
|
|
||||||
// Actions test = new Actions();
|
|
||||||
ShangTest test = new ShangTest();
|
|
||||||
test.init();
|
|
||||||
test.action();
|
|
||||||
test.end();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package com.sunyard.sydapi.loadrunner.action1;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* LoadRunner Java script. (Build: _build_number_)
|
|
||||||
*
|
|
||||||
* Script Description:
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
import racal.sunyard.main.SydApi4j;
|
|
||||||
import racal.sunyard.main.SydApiBuilder;
|
|
||||||
|
|
||||||
public class Actions
|
|
||||||
{
|
|
||||||
|
|
||||||
private static final byte[] testData = new byte[ 4 * 1024 ];
|
|
||||||
|
|
||||||
private SydApi4j api = null;
|
|
||||||
|
|
||||||
public int init() throws Throwable {
|
|
||||||
if ( null == api ) {
|
|
||||||
api = (SydApi4j) new SydApiBuilder()
|
|
||||||
.setIp("172.16.18.59")
|
|
||||||
.setPort( 8889 )
|
|
||||||
.useDebugMode()
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}//end of init
|
|
||||||
|
|
||||||
|
|
||||||
public int action() throws Throwable {
|
|
||||||
api.SM3Hash( testData );
|
|
||||||
return 0;
|
|
||||||
}//end of action
|
|
||||||
|
|
||||||
|
|
||||||
public int end() throws Throwable {
|
|
||||||
if (null != this.api ) {
|
|
||||||
this.api.disconnect();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}//end of end
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
package com.sunyard.sydapi.loadrunner.raw;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* LoadRunner Java script. (Build: _build_number_)
|
|
||||||
*
|
|
||||||
* Script Description:
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import lrapi.lr;
|
|
||||||
import racal.sunyard.main.SydApi4j;
|
|
||||||
import racal.sunyard.main.SydApiBuilder;
|
|
||||||
import com.sunyard.proto.Util;
|
|
||||||
import com.sunyard.SydApi;
|
|
||||||
|
|
||||||
public class Actions
|
|
||||||
{
|
|
||||||
|
|
||||||
private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346";
|
|
||||||
|
|
||||||
private static String pk ;
|
|
||||||
private static String sk ;
|
|
||||||
private SydApi api;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public int init() throws Throwable {
|
|
||||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.123");
|
|
||||||
api = builder.build();
|
|
||||||
|
|
||||||
this.pk = api.SM2GetPublicKeyC("123", 0 );
|
|
||||||
this.sk = api.SM2GetPrivateKeyC("123");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}//end of init
|
|
||||||
|
|
||||||
|
|
||||||
public int action() throws Throwable {
|
|
||||||
lr.start_transaction("auto");//设置事务开始点
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
System.out.println("begin SYD_Sign_P1");
|
|
||||||
byte[] sign = this.api.SM2SignC( 0 , sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
|
||||||
System.out.println("sign=" + Util.bytes2HexString( sign ));
|
|
||||||
|
|
||||||
System.out.println("begin SYD_Verify_P1");
|
|
||||||
boolean pass = api.SM2Verify(1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
|
||||||
System.out.println("验签结果 " + pass);
|
|
||||||
System.out.println("end SYD_Verify_P1");
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
lr.end_transaction("auto", lr.AUTO);//设置事务结束点
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}//end of action
|
|
||||||
|
|
||||||
|
|
||||||
public int end() throws Throwable {
|
|
||||||
return 0;
|
|
||||||
}//end of end
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Throwable {
|
|
||||||
|
|
||||||
Actions actions = new Actions();
|
|
||||||
actions.init();
|
|
||||||
actions.action();
|
|
||||||
actions.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
package com.sunyard.sydapi.loadrunner.raw_uid;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* LoadRunner Java script. (Build: _build_number_)
|
|
||||||
*
|
|
||||||
* Script Description:
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
import lrapi.lr;
|
|
||||||
import racal.sunyard.main.SydApi4j;
|
|
||||||
import racal.sunyard.main.SydApiBuilder;
|
|
||||||
import com.sunyard.proto.Util;
|
|
||||||
import com.sunyard.SydApi;
|
|
||||||
|
|
||||||
public class Actions
|
|
||||||
{
|
|
||||||
|
|
||||||
private static final String inHexData = "67c6697351ff4aec29cdbaabf2fbe346";
|
|
||||||
private static final String uid = "8765432187654321";
|
|
||||||
|
|
||||||
private static String pk ;
|
|
||||||
private static String sk ;
|
|
||||||
private SydApi api;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public int init() throws Throwable {
|
|
||||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.1.123");
|
|
||||||
api = builder.build();
|
|
||||||
|
|
||||||
this.pk = api.SM2GetPublicKeyC("123", 0 );
|
|
||||||
this.sk = api.SM2GetPrivateKeyC("123");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}//end of init
|
|
||||||
|
|
||||||
|
|
||||||
public int action() throws Throwable {
|
|
||||||
lr.start_transaction("auto");//设置事务开始点
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
System.out.println("begin SYD_Sign_P1");
|
|
||||||
byte[] sign = this.api.SM2SignC( 0, uid , sk, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData) );
|
|
||||||
System.out.println("sign=" + Util.bytes2HexString( sign ));
|
|
||||||
|
|
||||||
System.out.println("begin SYD_Verify_P1");
|
|
||||||
boolean pass = api.SM2Verify( uid, 1, Util.hexString2Bytes(pk), Util.hexString2Bytes(inHexData), sign );
|
|
||||||
System.out.println("验签结果 " + pass);
|
|
||||||
System.out.println("end SYD_Verify_P1");
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
lr.end_transaction("auto", lr.AUTO);//设置事务结束点
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}//end of action
|
|
||||||
|
|
||||||
|
|
||||||
public int end() throws Throwable {
|
|
||||||
return 0;
|
|
||||||
}//end of end
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Throwable {
|
|
||||||
|
|
||||||
Actions actions = new Actions();
|
|
||||||
actions.init();
|
|
||||||
actions.action();
|
|
||||||
actions.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
package com.jmeter.test;
|
package jmeter;
|
||||||
|
|
||||||
import com.Sample.SydUtils;
|
import sample.SydUtils;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.sunyard.SydApiException;
|
import com.sunyard.SydApiException;
|
||||||
import com.sunyard.entity.ImportResult;
|
import com.sunyard.entity.ImportResult;
|
||||||
@@ -17,7 +17,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
public class Test4Jemter extends AbstractJavaSamplerClient {
|
public class Test4Jmeter extends AbstractJavaSamplerClient {
|
||||||
|
|
||||||
private String SM2dn ;
|
private String SM2dn ;
|
||||||
private String RSAdn ;
|
private String RSAdn ;
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package lrapi;
|
|
||||||
|
|
||||||
public class lr {
|
|
||||||
|
|
||||||
public static String AUTO = "AUTO";
|
|
||||||
|
|
||||||
public static void start_transaction(String t) {
|
|
||||||
System.out.println("start_transaction" + t);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void end_transaction(String t, String type) {
|
|
||||||
System.out.println("end_transaction" + t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
package sample;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.sunyard.RetWrap;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CertKit {
|
||||||
|
|
||||||
|
private static String ip1;
|
||||||
|
private static String ip2;
|
||||||
|
private static int port;
|
||||||
|
private static int timeout = 3000;
|
||||||
|
|
||||||
|
private static SydApi4j api;
|
||||||
|
|
||||||
|
private static void printHelp() {
|
||||||
|
System.out.println("CertKit - 证书管理工具");
|
||||||
|
System.out.println("用法: java -jar CertKit.jar <命令> [参数]");
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("命令列表:");
|
||||||
|
System.out.println(" help 显示此帮助信息");
|
||||||
|
System.out.println(" import <证书文件路径> [机构号] 上传证书到加密设备");
|
||||||
|
System.out.println(" delete <证书DN> 从加密设备删除指定证书");
|
||||||
|
System.out.println(" list 列出加密设备中的所有证书");
|
||||||
|
System.out.println(" set <key> <value> 设置配置项并保存到 config.json");
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("可用配置项:");
|
||||||
|
System.out.println(" ip1 加密设备主IP地址");
|
||||||
|
System.out.println(" ip2 加密设备备IP地址");
|
||||||
|
System.out.println(" port 加密设备端口号");
|
||||||
|
System.out.println(" timeout 连接超时时间(毫秒)");
|
||||||
|
System.out.println(" dn 默认证书DN");
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("配置文件: config.json (需放在当前目录)");
|
||||||
|
System.out.println(" { \"ip1\": \"172.1.41.3\", \"ip2\": \"172.1.41.139\", \"port\": 8889 }");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void loadConfig() {
|
||||||
|
String workDir = System.getProperty("user.dir");
|
||||||
|
Path filePath = Paths.get(workDir, "config.json");
|
||||||
|
if (FileUtil.exist(filePath.toFile())) {
|
||||||
|
String configStr = FileUtil.readString(filePath.toFile(), StandardCharsets.UTF_8);
|
||||||
|
JSONObject config = JSONUtil.parseObj(configStr);
|
||||||
|
ip1 = config.getStr("ip1");
|
||||||
|
ip2 = config.getStr("ip2");
|
||||||
|
port = config.getInt("port");
|
||||||
|
System.out.println("配置已加载: ip1=" + ip1 + ", ip2=" + ip2 + ", port=" + port);
|
||||||
|
} else {
|
||||||
|
System.err.println("警告: 未找到配置文件 config.json,使用默认值");
|
||||||
|
ip1 = "172.1.41.3";
|
||||||
|
ip2 = "172.1.41.139";
|
||||||
|
port = 8889;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void connect() {
|
||||||
|
SydApi4j.SYD_Set_Parameter_HA(ip1, ip2, port, timeout);
|
||||||
|
api = (SydApi4j) new SydApi4j();
|
||||||
|
System.out.println("已连接到加密设备");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void disconnect() {
|
||||||
|
if (api != null) {
|
||||||
|
api.disconnect();
|
||||||
|
System.out.println("已断开连接");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void importCert(String certFilePath, String organizationId) {
|
||||||
|
try {
|
||||||
|
if (!FileUtil.exist(certFilePath)) {
|
||||||
|
System.err.println("错误: 证书文件不存在 - " + certFilePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] certData = FileUtil.readBytes(certFilePath);
|
||||||
|
byte[] orgIdBytes = organizationId != null ? organizationId.getBytes(StandardCharsets.UTF_8) : null;
|
||||||
|
|
||||||
|
RetWrap result = api.importCert(certData, orgIdBytes);
|
||||||
|
System.out.println("证书上传成功");
|
||||||
|
if (result != null) {
|
||||||
|
System.out.println("返回结果: " + result.toString());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("证书上传失败: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void deleteCert(String dn) {
|
||||||
|
try {
|
||||||
|
boolean result = api.deleteCert(dn);
|
||||||
|
if (result) {
|
||||||
|
System.out.println("证书删除成功");
|
||||||
|
} else {
|
||||||
|
System.out.println("证书删除失败");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("证书删除失败: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void listCerts() {
|
||||||
|
try {
|
||||||
|
List<String> certs = api.getAllCert();
|
||||||
|
System.out.println("证书列表 (" + certs.size() + " 个):");
|
||||||
|
for (int i = 0; i < certs.size(); i++) {
|
||||||
|
System.out.println((i + 1) + ". " + certs.get(i));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("获取证书列表失败: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setConfig(String key, String value) {
|
||||||
|
String workDir = System.getProperty("user.dir");
|
||||||
|
Path filePath = Paths.get(workDir, "config.json");
|
||||||
|
|
||||||
|
JSONObject config;
|
||||||
|
if (FileUtil.exist(filePath.toFile())) {
|
||||||
|
String configStr = FileUtil.readString(filePath.toFile(), StandardCharsets.UTF_8);
|
||||||
|
config = JSONUtil.parseObj(configStr);
|
||||||
|
} else {
|
||||||
|
config = new JSONObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
config.set(key, value);
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileUtil.writeString(JSONUtil.toJsonStr(config), filePath.toFile(), StandardCharsets.UTF_8);
|
||||||
|
System.out.println("配置已保存: " + key + " = " + value);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("保存配置失败: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length == 0) {
|
||||||
|
printHelp();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
String command = args[0].toLowerCase();
|
||||||
|
|
||||||
|
if ("help".equals(command)) {
|
||||||
|
printHelp();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("set".equals(command)) {
|
||||||
|
if (args.length < 3) {
|
||||||
|
System.err.println("错误: set 命令需要 key 和 value 参数");
|
||||||
|
System.err.println("用法: set <key> <value>");
|
||||||
|
} else {
|
||||||
|
String key = args[1];
|
||||||
|
String value = args[2];
|
||||||
|
setConfig(key, value);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
loadConfig();
|
||||||
|
|
||||||
|
try {
|
||||||
|
connect();
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
|
case "import":
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.err.println("错误: import 命令需要证书文件路径参数");
|
||||||
|
System.err.println("用法: import <证书文件路径> [机构号]");
|
||||||
|
} else {
|
||||||
|
String certFilePath = args[1];
|
||||||
|
String organizationId = args.length > 2 ? args[2] : null;
|
||||||
|
importCert(certFilePath, organizationId);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "delete":
|
||||||
|
if (args.length < 2) {
|
||||||
|
System.err.println("错误: delete 命令需要证书DN参数");
|
||||||
|
System.err.println("用法: delete <证书DN>");
|
||||||
|
} else {
|
||||||
|
String dn = args[1];
|
||||||
|
deleteCert(dn);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "list":
|
||||||
|
listCerts();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.err.println("未知命令: " + command);
|
||||||
|
printHelp();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("执行命令失败: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.Sample;
|
package sample;
|
||||||
|
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.sample;
|
package sample;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
import cn.hutool.core.date.StopWatch;
|
import cn.hutool.core.date.StopWatch;
|
||||||
import com.Sample.SydUtils;
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.sunyard.RetWrap;
|
|
||||||
import com.sunyard.SydApi;
|
|
||||||
import com.sunyard.SydApiException;
|
import com.sunyard.SydApiException;
|
||||||
import com.sunyard.entity.ImportResult;
|
import com.sunyard.entity.ImportResult;
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.Sample;
|
package sample;
|
||||||
|
|
||||||
import com.sunyard.RetWrap;
|
import com.sunyard.RetWrap;
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
Reference in New Issue
Block a user