添加大数据加密指令
This commit is contained in:
parent
e8a62c1d4a
commit
914ba8eee5
22
pom.xml
22
pom.xml
@ -34,6 +34,28 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Source: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
<version>1.65</version>
|
||||||
|
<!-- <scope>compile</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>
|
||||||
<groupId>com.sunyard</groupId>
|
<groupId>com.sunyard</groupId>
|
||||||
<artifactId>proto</artifactId>
|
<artifactId>proto</artifactId>
|
||||||
|
|||||||
42
src/main/java/com/sunyard/entity/MutiReturn7.java
Normal file
42
src/main/java/com/sunyard/entity/MutiReturn7.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package com.sunyard.entity;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
public class MutiReturn7 {
|
||||||
|
// 返回码
|
||||||
|
private int ret;
|
||||||
|
// 0-不是最后 1-是最后
|
||||||
|
private int isLast;
|
||||||
|
// 数据
|
||||||
|
private ByteBuffer data;
|
||||||
|
|
||||||
|
public MutiReturn7(int ret, int isLast, ByteBuffer data) {
|
||||||
|
this.ret = ret;
|
||||||
|
this.isLast = isLast;
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRet() {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRet(int ret) {
|
||||||
|
this.ret = ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIsLast() {
|
||||||
|
return isLast;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsLast(int isLast) {
|
||||||
|
this.isLast = isLast;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ByteBuffer getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setData(ByteBuffer data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
src/main/java/com/sunyard/util/ByteBufferUtil.java
Normal file
44
src/main/java/com/sunyard/util/ByteBufferUtil.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.sunyard.util;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
public class ByteBufferUtil {
|
||||||
|
public static ByteBuffer sliceRange(ByteBuffer source, int start, int length) {
|
||||||
|
// 保存原 buffer 的 position 和 limit
|
||||||
|
int oldPos = source.position();
|
||||||
|
int oldLimit = source.limit();
|
||||||
|
|
||||||
|
// 设置新的 position 和 limit 为目标区间
|
||||||
|
source.position(start);
|
||||||
|
source.limit(start + length);
|
||||||
|
|
||||||
|
// 创建共享视图(零拷贝)
|
||||||
|
ByteBuffer slice = source.slice();
|
||||||
|
|
||||||
|
// 恢复原 buffer 的 position 和 limit
|
||||||
|
source.position(oldPos);
|
||||||
|
source.limit(oldLimit);
|
||||||
|
|
||||||
|
return slice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ByteBuffer sliceAndConsume(ByteBuffer source, int length) {
|
||||||
|
// 保存原 buffer 的 position 和 limit
|
||||||
|
int oldPos = source.position();
|
||||||
|
int oldLimit = source.limit();
|
||||||
|
|
||||||
|
// 设置新的 position 和 limit 为目标区间
|
||||||
|
int start = oldPos;
|
||||||
|
source.position(start);
|
||||||
|
source.limit(start + length);
|
||||||
|
|
||||||
|
// 创建共享视图(零拷贝)
|
||||||
|
ByteBuffer slice = source.slice();
|
||||||
|
|
||||||
|
// 恢复原 buffer 的 position 和 limit
|
||||||
|
source.position(oldPos + length);
|
||||||
|
source.limit(oldLimit);
|
||||||
|
|
||||||
|
return slice;
|
||||||
|
}
|
||||||
|
}
|
||||||
367
src/main/java/com/sunyard/util/ParamChecker.java
Normal file
367
src/main/java/com/sunyard/util/ParamChecker.java
Normal file
@ -0,0 +1,367 @@
|
|||||||
|
package com.sunyard.util;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 参数检查工具类,提供常见的参数校验方法。
|
||||||
|
* <p>
|
||||||
|
* 所有方法在校验失败时均抛出 {@link IllegalArgumentException} 异常,
|
||||||
|
* 并附带包含参数名的明确错误信息。成功时返回被校验的值,便于链式调用。
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>使用示例:
|
||||||
|
* <pre>{@code
|
||||||
|
* public void setAge(int age) {
|
||||||
|
* ParamChecker.checkInRange(age, "age", 0, 150);
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public void setName(String name) {
|
||||||
|
* ParamChecker.checkNotBlank(name, "name");
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public void setStatus(int status) {
|
||||||
|
* ParamChecker.checkInValues(status, "status", 1, 2, 3);
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author Cheney
|
||||||
|
*/
|
||||||
|
public final class ParamChecker {
|
||||||
|
|
||||||
|
private ParamChecker() {
|
||||||
|
// 私有构造器,防止实例化
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查表达式是否为真,若为假则抛出异常。
|
||||||
|
*
|
||||||
|
* @param expression 待检查的布尔表达式
|
||||||
|
* @param message 异常信息
|
||||||
|
* @throws IllegalArgumentException 如果 expression 为 false
|
||||||
|
*/
|
||||||
|
public static void checkArgument(boolean expression, String message) {
|
||||||
|
if (!expression) {
|
||||||
|
throw new IllegalArgumentException(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 对象非空检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查对象不为 null。
|
||||||
|
*
|
||||||
|
* @param obj 待检查的对象
|
||||||
|
* @param paramName 参数名称(用于异常信息)
|
||||||
|
* @param <T> 对象类型
|
||||||
|
* @return 传入的对象(便于链式调用)
|
||||||
|
* @throws IllegalArgumentException 如果 obj 为 null
|
||||||
|
*/
|
||||||
|
public static <T> T checkNotNull(T obj, String paramName) {
|
||||||
|
if (obj == null) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null");
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 字符串检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查字符串不为 null 且长度大于 0(不忽略空白字符)。
|
||||||
|
*
|
||||||
|
* @param str 待检查的字符串
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的字符串
|
||||||
|
* @throws IllegalArgumentException 如果 str 为 null 或空字符串
|
||||||
|
*/
|
||||||
|
public static String checkNotEmpty(String str, String paramName) {
|
||||||
|
if (str == null || str.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空字符串");
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查字符串不为 null 且去除首尾空白后长度大于 0。
|
||||||
|
*
|
||||||
|
* @param str 待检查的字符串
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的字符串(原值,非 trim 后结果)
|
||||||
|
* @throws IllegalArgumentException 如果 str 为 null 或仅包含空白字符
|
||||||
|
*/
|
||||||
|
public static String checkNotBlank(String str, String paramName) {
|
||||||
|
if (str == null || str.isBlank()) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null、空字符串或仅包含空白字符");
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查字符串长度在指定范围内(包含边界)。
|
||||||
|
*
|
||||||
|
* @param str 待检查的字符串
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param min 最小长度(包含)
|
||||||
|
* @param max 最大长度(包含)
|
||||||
|
* @return 传入的字符串
|
||||||
|
* @throws IllegalArgumentException 如果 str 为 null,或长度不在 [min, max] 范围内
|
||||||
|
*/
|
||||||
|
public static String checkLengthBetween(String str, String paramName, int min, int max) {
|
||||||
|
checkNotNull(str, paramName);
|
||||||
|
if (min > max) {
|
||||||
|
throw new IllegalArgumentException("内部错误:长度检查的最小值 " + min + " 不能大于最大值 " + max);
|
||||||
|
}
|
||||||
|
int len = str.length();
|
||||||
|
if (len < min || len > max) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("参数 '%s' 的长度 %d 不在允许范围 [%d, %d] 内", paramName, len, min, max));
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 整数(int)检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 int 值大于 0。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value <= 0
|
||||||
|
*/
|
||||||
|
public static int checkPositive(int value, String paramName) {
|
||||||
|
if (value <= 0) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 int 值 >= 0。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value < 0
|
||||||
|
*/
|
||||||
|
public static int checkNonNegative(int value, String paramName) {
|
||||||
|
if (value < 0) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数,当前值: " + value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 int 值在指定范围内(包含边界)。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value 不在 [min, max] 范围内,或 min > max
|
||||||
|
*/
|
||||||
|
public static int checkInRange(int value, String paramName, int min, int max) {
|
||||||
|
if (min > max) {
|
||||||
|
throw new IllegalArgumentException("内部错误:范围检查的最小值 " + min + " 不能大于最大值 " + max);
|
||||||
|
}
|
||||||
|
if (value < min || value > max) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("参数 '%s' 的值 %d 不在允许范围 [%d, %d] 内", paramName, value, min, max));
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 int 值是否等于给定集合中的任意一个值。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param validValues 允许的值列表
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value 不在 validValues 中,或 validValues 为空
|
||||||
|
*/
|
||||||
|
public static int checkInValues(int value, String paramName, int... validValues) {
|
||||||
|
if (validValues == null || validValues.length == 0) {
|
||||||
|
throw new IllegalArgumentException("内部错误:允许的值集合不能为空");
|
||||||
|
}
|
||||||
|
for (int v : validValues) {
|
||||||
|
if (value == v) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 构建允许值字符串用于提示
|
||||||
|
StringBuilder allowed = new StringBuilder();
|
||||||
|
for (int i = 0; i < validValues.length; i++) {
|
||||||
|
if (i > 0) allowed.append(", ");
|
||||||
|
allowed.append(validValues[i]);
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("参数 '%s' 的值 %d 不在允许值集合 [%s] 中", paramName, value, allowed));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 长整型(long)检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 long 值大于 0。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value <= 0
|
||||||
|
*/
|
||||||
|
public static long checkPositive(long value, String paramName) {
|
||||||
|
if (value <= 0) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 long 值 >= 0。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value < 0
|
||||||
|
*/
|
||||||
|
public static long checkNonNegative(long value, String paramName) {
|
||||||
|
if (value < 0) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数,当前值: " + value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 long 值在指定范围内(包含边界)。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param min 最小值(包含)
|
||||||
|
* @param max 最大值(包含)
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value 不在 [min, max] 范围内,或 min > max
|
||||||
|
*/
|
||||||
|
public static long checkInRange(long value, String paramName, long min, long max) {
|
||||||
|
if (min > max) {
|
||||||
|
throw new IllegalArgumentException("内部错误:范围检查的最小值 " + min + " 不能大于最大值 " + max);
|
||||||
|
}
|
||||||
|
if (value < min || value > max) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("参数 '%s' 的值 %d 不在允许范围 [%d, %d] 内", paramName, value, min, max));
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 浮点数(double)简单检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 double 值大于 0(考虑极小正数,不做精度处理)。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value <= 0 或为 NaN
|
||||||
|
*/
|
||||||
|
public static double checkPositive(double value, String paramName) {
|
||||||
|
if (value <= 0 || Double.isNaN(value)) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 必须为正数,当前值: " + value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 double 值 >= 0。
|
||||||
|
*
|
||||||
|
* @param value 待检查的值
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @return 传入的值
|
||||||
|
* @throws IllegalArgumentException 如果 value < 0 或为 NaN
|
||||||
|
*/
|
||||||
|
public static double checkNonNegative(double value, String paramName) {
|
||||||
|
if (value < 0 || Double.isNaN(value)) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为负数或 NaN,当前值: " + value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 集合与数组非空检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查集合不为 null 且非空。
|
||||||
|
*
|
||||||
|
* @param coll 待检查的集合
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param <T> 集合元素类型
|
||||||
|
* @return 传入的集合
|
||||||
|
* @throws IllegalArgumentException 如果 coll 为 null 或空集合
|
||||||
|
*/
|
||||||
|
public static <T> Collection<T> checkNotEmpty(Collection<T> coll, String paramName) {
|
||||||
|
if (coll == null || coll.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空集合");
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查 Map 不为 null 且非空。
|
||||||
|
*
|
||||||
|
* @param map 待检查的 Map
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param <K> 键类型
|
||||||
|
* @param <V> 值类型
|
||||||
|
* @return 传入的 Map
|
||||||
|
* @throws IllegalArgumentException 如果 map 为 null 或空 Map
|
||||||
|
*/
|
||||||
|
public static <K, V> Map<K, V> checkNotEmpty(Map<K, V> map, String paramName) {
|
||||||
|
if (map == null || map.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空 Map");
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查数组不为 null 且长度大于 0。
|
||||||
|
*
|
||||||
|
* @param array 待检查的数组
|
||||||
|
* @param paramName 参数名称
|
||||||
|
* @param <T> 数组元素类型
|
||||||
|
* @return 传入的数组
|
||||||
|
* @throws IllegalArgumentException 如果 array 为 null 或长度为 0
|
||||||
|
*/
|
||||||
|
public static <T> T[] checkNotEmpty(T[] array, String paramName) {
|
||||||
|
if (array == null || array.length == 0) {
|
||||||
|
throw new IllegalArgumentException("参数 '" + paramName + "' 不能为 null 或空数组");
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== 布尔条件检查 ====================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查布尔条件为 true。
|
||||||
|
*
|
||||||
|
* @param condition 待检查的条件
|
||||||
|
* @param paramName 参数名称(描述条件含义)
|
||||||
|
* @throws IllegalArgumentException 如果 condition 为 false
|
||||||
|
*/
|
||||||
|
public static void checkTrue(boolean condition, String paramName) {
|
||||||
|
if (!condition) {
|
||||||
|
throw new IllegalArgumentException("参数条件 '" + paramName + "' 必须为 true");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查布尔条件为 false。
|
||||||
|
*
|
||||||
|
* @param condition 待检查的条件
|
||||||
|
* @param paramName 参数名称(描述条件含义)
|
||||||
|
* @throws IllegalArgumentException 如果 condition 为 true
|
||||||
|
*/
|
||||||
|
public static void checkFalse(boolean condition, String paramName) {
|
||||||
|
if (condition) {
|
||||||
|
throw new IllegalArgumentException("参数条件 '" + paramName + "' 必须为 false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
|
|||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
import cn.hutool.core.date.StopWatch;
|
import cn.hutool.core.date.StopWatch;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.ByteUtil;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.sunyard.RetWrap;
|
import com.sunyard.RetWrap;
|
||||||
@ -13,6 +14,7 @@ import com.sunyard.SydApiException;
|
|||||||
import com.sunyard.cert.X509;
|
import com.sunyard.cert.X509;
|
||||||
import com.sunyard.constant.CertUsage;
|
import com.sunyard.constant.CertUsage;
|
||||||
import com.sunyard.entity.ImportResult;
|
import com.sunyard.entity.ImportResult;
|
||||||
|
import com.sunyard.entity.MutiReturn7;
|
||||||
import com.sunyard.entity.Struct;
|
import com.sunyard.entity.Struct;
|
||||||
import com.sunyard.log.ILogFactory;
|
import com.sunyard.log.ILogFactory;
|
||||||
import com.sunyard.log.ILogger;
|
import com.sunyard.log.ILogger;
|
||||||
@ -6825,6 +6827,134 @@ public class SydApi4j implements SydApi {
|
|||||||
return SYMUtil.derEnData2Base64EnC1C2C3(((PacketSection) ret.get("cipherData")).getBytes());
|
return SYMUtil.derEnData2Base64EnC1C2C3(((PacketSection) ret.get("cipherData")).getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData){
|
||||||
|
ParamChecker.checkNotNull(publicKey, "publicKey");
|
||||||
|
ParamChecker.checkNotNull(pOrgData, "pOrgData");
|
||||||
|
|
||||||
|
final int CHUNK_SIZE = 63 * 1024; // 64 KB
|
||||||
|
int dataLen = pOrgData.length;
|
||||||
|
int offset = 0;
|
||||||
|
|
||||||
|
while (offset < dataLen) {
|
||||||
|
int len = Math.min(CHUNK_SIZE, dataLen - offset);
|
||||||
|
// 调用底层支持偏移量的加密方法
|
||||||
|
SYD_SM2Encrypt(1, publicKey, ByteBuffer.wrap(pOrgData, offset, len), offset);
|
||||||
|
offset += len;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算
|
||||||
|
SYD_SM2Encrypt(2, publicKey, null, 0);
|
||||||
|
|
||||||
|
// 取回
|
||||||
|
int idx = 0;
|
||||||
|
List<ByteBuffer> chunks = new ArrayList<>();
|
||||||
|
int totalLen = 0;
|
||||||
|
int isLast = 0;
|
||||||
|
|
||||||
|
while (isLast != 1) {
|
||||||
|
MutiReturn7 ret = SYD_SM2Encrypt(3, null, null, idx);
|
||||||
|
ByteBuffer bb = ret.getData();
|
||||||
|
isLast = ret.getIsLast();
|
||||||
|
|
||||||
|
if (bb != null && bb.remaining() > 0) {
|
||||||
|
int remaining = bb.remaining();
|
||||||
|
chunks.add(bb); // 直接存储,不需要 duplicate
|
||||||
|
totalLen += remaining;
|
||||||
|
idx += remaining;
|
||||||
|
} else if (isLast != 1) {
|
||||||
|
// 没有数据但还不是最后一块,异常
|
||||||
|
throw new RuntimeException("Empty chunk with isLast=0");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] result = new byte[totalLen];
|
||||||
|
int offset2 = 0;
|
||||||
|
for (ByteBuffer chunk : chunks) {
|
||||||
|
int len = chunk.remaining();
|
||||||
|
chunk.get(result, offset2, len);
|
||||||
|
offset2 += len;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// 支持大包加解密
|
||||||
|
private MutiReturn7 SYD_SM2Encrypt(int dataType, byte[] publicKey, ByteBuffer pOrgData, int idx) {
|
||||||
|
// 参数检查
|
||||||
|
ParamChecker.checkInRange(dataType, "dataType", 1, 3);
|
||||||
|
|
||||||
|
// 最大缓存 64K
|
||||||
|
// 根据输入数据快速估算缓存大小
|
||||||
|
int buffLen = 4096;
|
||||||
|
if( 1 == dataType ) {
|
||||||
|
buffLen = pOrgData.limit() > 4096 ? 64*1024 : 5*1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ByteBuffer bb = ByteBuffer.allocate(buffLen );
|
||||||
|
PacketSN sn = PacketSN.gen();
|
||||||
|
bb.put(new byte[2]);
|
||||||
|
bb.put(sn.getSn().array());
|
||||||
|
bb.put("7F".getBytes());
|
||||||
|
bb.put((byte) dataType); // 数据包标志
|
||||||
|
if ( 2 == dataType ) { // 公钥
|
||||||
|
bb.put(ByteUtil.shortToBytes((short) publicKey.length));
|
||||||
|
bb.put(publicKey);
|
||||||
|
}
|
||||||
|
if ( 1 == dataType ) { // 数据
|
||||||
|
bb.put(ByteUtil.shortToBytes((short) pOrgData.limit()));
|
||||||
|
bb.put(pOrgData);
|
||||||
|
bb.put(ByteUtil.shortToBytes((short)idx));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 3 == dataType) { // 仅数据长度
|
||||||
|
bb.put( ByteUtil.shortToBytes((short) (63*1024)) );
|
||||||
|
bb.put( ByteUtil.shortToBytes((short)idx));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 重新计算长度
|
||||||
|
int len = bb.position();
|
||||||
|
len -= 2;
|
||||||
|
byte[] lenArray = new byte[2];
|
||||||
|
lenArray[0] = (byte) ((len >> 8) & 0xFF);
|
||||||
|
lenArray[1] = (byte) (len & 0xFF);
|
||||||
|
bb.put(lenArray);
|
||||||
|
|
||||||
|
// 通信
|
||||||
|
synchronized (this) {
|
||||||
|
// 响应解析
|
||||||
|
bb = syncRead(syncSend(bb));
|
||||||
|
}
|
||||||
|
|
||||||
|
bb.flip();
|
||||||
|
bb.position(12);
|
||||||
|
byte[] code = new byte[2];
|
||||||
|
bb.get(code);
|
||||||
|
|
||||||
|
int retCode = Integer.valueOf(new String(code));
|
||||||
|
if (0 != retCode) {
|
||||||
|
throw new SydApiException(retCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只有类型是 3 时有数据结果
|
||||||
|
if ( 3 == dataType ){
|
||||||
|
int isLast = bb.get();
|
||||||
|
byte[] dataLenArr = new byte[2];
|
||||||
|
bb.get(dataLenArr);
|
||||||
|
int dataLen = ByteUtil.bytesToInt(dataLenArr);
|
||||||
|
// byte[] data = new byte[dataLen];
|
||||||
|
// bb.get(data);
|
||||||
|
ByteBuffer slice = ByteBufferUtil.sliceAndConsume(bb, dataLen);
|
||||||
|
return new MutiReturn7(retCode, isLast, slice);
|
||||||
|
} else {
|
||||||
|
return new MutiReturn7(retCode, 0, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String SM2Encrypt(byte[] publicKey, byte[] pOrgData) {
|
public String SM2Encrypt(byte[] publicKey, byte[] pOrgData) {
|
||||||
Proto77 proto = new Proto77();
|
Proto77 proto = new Proto77();
|
||||||
|
|||||||
72
src/test/java/cmbpoc/FunctionTest.java
Normal file
72
src/test/java/cmbpoc/FunctionTest.java
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package cmbpoc;
|
||||||
|
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
public class FunctionTest {
|
||||||
|
|
||||||
|
private SydApi4j api;
|
||||||
|
private String keypairIndex;
|
||||||
|
private byte[] privateKey;
|
||||||
|
private byte[] publicKey;
|
||||||
|
private byte[] orgData;
|
||||||
|
private String sign;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void start(){
|
||||||
|
// 调试模式
|
||||||
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
|
// 建立链接(单台)
|
||||||
|
this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void stop(){
|
||||||
|
if ( null != api ) {
|
||||||
|
this.api.disconnect();
|
||||||
|
this.api = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getPrivateKeyAndPublickKey(){
|
||||||
|
this.publicKey = Util.hexString2Bytes(
|
||||||
|
this.api.SM2GetPublicKeyC(keypairIndex)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
this.privateKey = Util.hexString2Bytes(
|
||||||
|
this.api.SM2GetPrivateKeyC(keypairIndex)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Sign(){
|
||||||
|
this.api.SYD_SM2_Sign_HA(1, privateKey, publicKey, orgData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Verify(){
|
||||||
|
this.api.SYD_SM2_Verify_HA(1, publicKey, orgData, sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SYD_SM2_Encrypt(){
|
||||||
|
this.api.SM2Decrypt();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -22,7 +22,7 @@ public class CertTest {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
SydApi4j api = (SydApi4j) new SydApi4j().connect("172.16.18.5", 8889, null, 1000);
|
SydApi4j api = (SydApi4j) new SydApi4j().connect("192.168.1.129", 8889, null, 1000);
|
||||||
try{
|
try{
|
||||||
List<String>dns = api.getAllCert();
|
List<String>dns = api.getAllCert();
|
||||||
System.out.println(Arrays.toString( dns.toArray() ) );
|
System.out.println(Arrays.toString( dns.toArray() ) );
|
||||||
|
|||||||
@ -8,54 +8,55 @@ import racal.sunyard.main.SydApiBuilder;
|
|||||||
public class TestShangqingXingye {
|
public class TestShangqingXingye {
|
||||||
private static final byte[] orgDataBig = new byte[ 5 * 1024];
|
private static final byte[] orgDataBig = new byte[ 5 * 1024];
|
||||||
private static final byte[] orgData2k = new byte[ 2 * 1024];
|
private static final byte[] orgData2k = new byte[ 2 * 1024];
|
||||||
private static final String pcDnEN = "C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1";
|
private static final String pcDnEN = "C=CN,O=CFCA TEST SM2 OCA31,OU=SGE,OU=Organizational-2,CN=SGE051@7028-702821-b70280002@Z000000001@1";
|
||||||
|
private static final String pcDnEN2 = "C=CN,O=OCA31SM2,OU=Local RA OCA31,OU=Organizational-2,CN=CFCA@sunyard@N91330108762038374D@52";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
long startTime = System.currentTimeMillis(); //获取开始时间
|
long startTime = System.currentTimeMillis(); //获取开始时间
|
||||||
SydApiBuilder builder = new SydApiBuilder().setIp("192.168.0.100");
|
SydApiBuilder builder = new SydApiBuilder().setIp("172.1.41.214");
|
||||||
SydApi4j api = (SydApi4j) builder.build();
|
SydApi4j api = (SydApi4j) builder.build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* 产生对称密钥,通过公钥加密生成数字信封,。
|
// * 产生对称密钥,通过公钥加密生成数字信封,。
|
||||||
* @param pcDnEN: 清算所的公钥证书
|
// * @param pcDnEN: 清算所的公钥证书
|
||||||
* @param pcData: 明文数据。
|
// * @param pcData: 明文数据。
|
||||||
* @return RetWrap: 数字信封
|
// * @return RetWrap: 数字信封
|
||||||
*/
|
// */
|
||||||
RetWrap signData = api.generateDEByDn(pcDnEN, orgDataBig);
|
// RetWrap signData = api.generateDEByDn(pcDnEN, orgDataBig);
|
||||||
byte[] cpkey2 = (byte[]) signData.get("CipherKey");
|
// byte[] cpkey2 = (byte[]) signData.get("CipherKey");
|
||||||
System.out.println("加密信封:" + new String(cpkey2));
|
// System.out.println("加密信封:" + new String(cpkey2));
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 接收用私钥解密数字信封,还原原文。。
|
// * 接收用私钥解密数字信封,还原原文。。
|
||||||
* @param pcDnEN: 清算所的公钥证书
|
// * @param pcDnEN: 清算所的公钥证书
|
||||||
* @param cpkey: 数字信封。
|
// * @param cpkey: 数字信封。
|
||||||
* @return preData: 原数据
|
// * @return preData: 原数据
|
||||||
*/
|
// */
|
||||||
RetWrap oDataWrap = api.decryptDEByDN(pcDnEN, cpkey2);
|
// RetWrap oDataWrap = api.decryptDEByDN(pcDnEN, cpkey2);
|
||||||
byte[] preData = (byte[]) oDataWrap.get("oData");
|
// byte[] preData = (byte[]) oDataWrap.get("oData");
|
||||||
System.out.println("解密信封:" + Util.bytes2HexString(preData));
|
// System.out.println("解密信封:" + Util.bytes2HexString(preData));
|
||||||
|
//
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 通过指定的私钥对指定的原始数据编制带公钥证书的数字签名(遵循PKCS#7),
|
// * 通过指定的私钥对指定的原始数据编制带公钥证书的数字签名(遵循PKCS#7),
|
||||||
* @param orgData: 待签名的原始数据
|
// * @param orgData: 待签名的原始数据
|
||||||
* @param pcDnEN: 签名者证书 DN
|
// * @param pcDnEN: 签名者证书 DN
|
||||||
* @return signsData: 签名数据
|
// * @return signsData: 签名数据
|
||||||
*/
|
// */
|
||||||
String signsData = api.SYD_SM2_DetachedSign_HA(orgData2k, pcDnEN);
|
// String signsData = api.SYD_SM2_DetachedSign_HA(orgData2k, pcDnEN);
|
||||||
System.out.println("签名数据:" + signsData);
|
// System.out.println("签名数据:" + signsData);
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 利用清算所的公钥证书对签名数据进行验签。
|
// * 利用清算所的公钥证书对签名数据进行验签。
|
||||||
* @param dataType: 数据模式, 0:输入的数据为 HASH 后的数据, 1:输入的数据为原始数据库(本次传入1)
|
// * @param dataType: 数据模式, 0:输入的数据为 HASH 后的数据, 1:输入的数据为原始数据库(本次传入1)
|
||||||
* @param orgData: 待签名的原始数据
|
// * @param orgData: 待签名的原始数据
|
||||||
* @param sign: 签名数据。
|
// * @param sign: 签名数据。
|
||||||
*/
|
// */
|
||||||
boolean result = api.detachedVerify(1, orgData2k, signsData);
|
// boolean result = api.detachedVerify(1, orgData2k, signsData);
|
||||||
System.out.println("验签结果:" + result);
|
// System.out.println("验签结果:" + result);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,7 +68,7 @@ public class TestShangqingXingye {
|
|||||||
* @param pcData: 明文数据。
|
* @param pcData: 明文数据。
|
||||||
* @return pcSignData: 签名数字信封
|
* @return pcSignData: 签名数字信封
|
||||||
*/
|
*/
|
||||||
String pcSignData = api.SYD_SignDigitalEnvelope(pcDnEN, pcDnEN, orgData2k);
|
String pcSignData = api.SYD_SignDigitalEnvelope(pcDnEN2, pcDnEN2, orgData2k);
|
||||||
System.out.println("签名数字信封:" + pcSignData);
|
System.out.println("签名数字信封:" + pcSignData);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -79,7 +80,7 @@ public class TestShangqingXingye {
|
|||||||
* @return pcData:原数据
|
* @return pcData:原数据
|
||||||
*/
|
*/
|
||||||
|
|
||||||
byte[] oData = api.SYD_VerifyDigitalEnveloe(pcDnEN, pcDnEN, pcSignData);
|
byte[] oData = api.SYD_VerifyDigitalEnveloe(pcDnEN2, pcDnEN2, pcSignData);
|
||||||
System.out.println("原数据:" + Util.bytes2HexString(oData));
|
System.out.println("原数据:" + Util.bytes2HexString(oData));
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user