sydapi-java-sge-database/src/main/java/com/sunyard/sge/database/SydApi4Database.java
2022-09-15 13:51:51 +08:00

216 lines
6.7 KiB
Java

package com.sunyard.sge.database;
import com.sunyard.sge.database.pool.HsmLinkInfo;
import racal.sunyard.main.SydApi4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.sunyard.sge.database.SydApiConfig.BATCH_SIZE;
/**
* 主要用于短连接
*/
public class SydApi4Database extends SydApiBaseFunction {
public SydApi4Database() {
super();
}
public SydApi4Database(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
super(hsms, linkInfos);
}
@Override
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
if ( null == iKeyIndex || null == pcData ) {
throw new IllegalArgumentException("批量输入数据为 null");
}
if ( iKeyIndex.length != pcData.size() ) {
throw new IllegalArgumentException("批量输入数据密钥和数据数量不等");
}
if ( 0 == iKeyIndex.length ) {
return new ArrayList<>();
}
// 将数据进行分组,防止超长。
List<List<byte[]>> dataGroups = new ArrayList<>();
List<List<Integer>> keyGroups = new ArrayList<>();
int totalLen = 0;
List<byte[]> curDataGroup = new ArrayList<>();
List<Integer> curKeyGroup = new ArrayList<>();
for (int i = 0; i < pcData.size(); i++) {
byte[] data = pcData.get(i);
if (null == data) {
data = new byte[0];
}
if (
curDataGroup.size() > 9 // 分组小于等于 9 个
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
) { // 扩展包
dataGroups.add(curDataGroup);
keyGroups.add(curKeyGroup);
curDataGroup = new ArrayList<>();
curKeyGroup = new ArrayList<>();
totalLen = 0;
}
curDataGroup.add(data);
curKeyGroup.add(iKeyIndex[i]);
totalLen += data.length;
}
dataGroups.add(curDataGroup);
keyGroups.add(curKeyGroup);
// 排队处理
List<byte[]> ret = new ArrayList<>();
for (int i = 0; i < dataGroups.size(); i++) {
ret.addAll(super.SYD_SM4_BatchData(toIntArray(keyGroups.get(i)), dataGroups.get(i), iFlag));
}
return ret;
}
@Override
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
if ( null == iKeyIndex || null == pcData ) {
throw new IllegalArgumentException("批量输入数据为 null");
}
if ( iKeyIndex.length != pcData.size() ) {
throw new IllegalArgumentException("批量输入数据密钥和数据数量不等");
}
if ( 0 == iKeyIndex.length ) {
return new String[0];
}
// 将数据进行分组,防止超长。
List<List<byte[]>> dataGroups = new ArrayList<>();
List<List<Integer>> keyGroups = new ArrayList<>();
int totalLen = 0;
List<byte[]> curDataGroup = new ArrayList<>();
List<Integer> curKeyGroup = new ArrayList<>();
for (int i = 0; i < pcData.size(); i++) {
byte[] data = pcData.get(i);
if (null == data) {
data = new byte[0];
}
if (
curDataGroup.size() > 9 // 分组小于等于 9 个
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
) {// 扩展包
dataGroups.add(curDataGroup);
keyGroups.add(curKeyGroup);
curDataGroup = new ArrayList<>();
curKeyGroup = new ArrayList<>();
totalLen = 0;
}
curDataGroup.add(data);
curKeyGroup.add(iKeyIndex[i]);
totalLen += data.length;
}
dataGroups.add(curDataGroup);
keyGroups.add(curKeyGroup);
// 排队处理
List<String> ret = new ArrayList<>();
for (int i = 0; i < dataGroups.size(); i++) {
String[] macs = super.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(i)), dataGroups.get(i));
ret.addAll(Arrays.asList(macs));
}
return ret.toArray(new String[0]);
}
@Override
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
if ( null == iKeyIndex || null == pcData ) {
throw new IllegalArgumentException("批量输入数据为 null");
}
if ( iKeyIndex.length != pcData.size() ) {
throw new IllegalArgumentException("批量输入数据密钥和数据数量不等");
}
if ( 0 == iKeyIndex.length ) {
return new boolean[0];
}
// 将数据进行分组,防止超长。
List<List<byte[]>> dataGroups = new ArrayList<>();
List<List<Integer>> keyGroups = new ArrayList<>();
List<List<String>> macGroups = new ArrayList<>();
int totalLen = 0;
List<byte[]> curDataGroup = new ArrayList<>();
List<Integer> curKeyGroup = new ArrayList<>();
List<String> curMacGroup = new ArrayList<>();
for (int i = 0; i < pcData.size(); i++) {
byte[] data = pcData.get(i);
if (null == data) {
data = new byte[0];
}
if (
curDataGroup.size() > 9 // 分组小于等于 9 个
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
) {
// 扩展包
dataGroups.add(curDataGroup);
keyGroups.add(curKeyGroup);
macGroups.add(curMacGroup);
curDataGroup = new ArrayList<>();
curKeyGroup = new ArrayList<>();
curMacGroup = new ArrayList<>();
totalLen = 0;
}
curDataGroup.add(data);
curKeyGroup.add(iKeyIndex[i]);
curMacGroup.add(pcMac[i]);
totalLen += data.length;
}
dataGroups.add(curDataGroup);
keyGroups.add(curKeyGroup);
macGroups.add(curMacGroup);
// 排队处理
List<Boolean> ret = new ArrayList<>();
for (int i = 0; i < dataGroups.size(); i++) {
boolean[] rets = super.SYD_SM4Mac_BatchData(
toIntArray(keyGroups.get(i)),
dataGroups.get(i),
macGroups.get(i).toArray(new String[0])
);
for (boolean r : rets) {
ret.add(r);
}
}
return toBoolArray(ret);
}
}