MAC 统一为 bytes

This commit is contained in:
cheney
2025-07-11 14:52:57 +08:00
parent 91cb26b03a
commit 9c212c4f83
18 changed files with 1009 additions and 758 deletions
@@ -1,6 +1,11 @@
package com.sunyard.sge.database;
import com.sun.jna.ptr.IntByReference;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import com.sunyard.sge.bytes.BytesUtil;
import com.sunyard.sge.plus.ConnectHandlerManager;
import com.sunyard.sge.plus.SydLibrary;
import java.util.ArrayList;
import java.util.Arrays;
@@ -45,6 +50,9 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -60,6 +68,9 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -75,6 +86,9 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -90,6 +104,9 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -106,6 +123,9 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -121,27 +141,33 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@Override
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
FutureTask<String> task = builder.getLongLinkWorks().fTask(new Callable<String>() {
public byte[] SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
FutureTask<byte[]> task = builder.getLongLinkWorks().fTask(new Callable<byte[]>() {
@Override
public String call() throws Exception {
public byte[] call() throws Exception {
return builder.get().SYD_SM4Mac_ShortData(iKeyIndex, pcData);
}
});
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@Override
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, byte[] pcMac) {
FutureTask<Boolean> task = builder.getLongLinkWorks().fTask(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
@@ -151,6 +177,9 @@ public class SydApiLongLinkWrap implements SydApi {
try {
return task.get();
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -166,16 +195,19 @@ public class SydApiLongLinkWrap implements SydApi {
byte[] enData = taskEnc.get();
FutureTask<String> taskMac = builder.getLongLinkWorks().fTask(new Callable<String>() {
FutureTask<byte[]> taskMac = builder.getLongLinkWorks().fTask(new Callable<byte[]>() {
@Override
public String call() throws Exception {
public byte[] call() throws Exception {
return builder.get().SYD_SM4Mac_ShortData(iMacKeyIndex, enData);
}
});
String mac = taskMac.get();
byte[] mac = taskMac.get();
return new DataAndMac(enData, mac);
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@@ -183,8 +215,40 @@ public class SydApiLongLinkWrap implements SydApi {
@Override
public DataAndMac SYD_SM4_EncryptAndMac_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData) {
try {
if ( ! ConnectHandlerManager.isInited() ) {
throw new SydApiException(-1);
}
return SYD_SM4_AllInOne(iEncKeyIndex, iMacKeyIndex, pcData);
int enLen = BytesUtil.calculatePaddedLength(pcData.length);
byte[] encData = new byte[enLen];
byte[] mac = new byte[16];
IntByReference encDataLen = new IntByReference(encData.length);
int i = SydLibrary.INSTANCE.SYD_SM4_Encrypt_Mac_ShortData(
ConnectHandlerManager.getHandler(),
iEncKeyIndex,
iMacKeyIndex,
pcData,
pcData.length,
encData,
encDataLen,
mac
);
if ( 0 == i ){
byte[] data = new byte[encDataLen.getValue()];
if( data.length == encData.length ) {
data = encData;
} else {
System.arraycopy(encData, 0, data, 0, data.length);
}
return new DataAndMac(data, mac);
} else {
throw new SydApiException(i);
}
// return SYD_SM4_AllInOne(iEncKeyIndex, iMacKeyIndex, pcData);
// // 线程池忙, 使用单线程计算
// if( builder.getLongLinkWorks().threadPoolIsBusy() ) {
@@ -245,53 +309,203 @@ public class SydApiLongLinkWrap implements SydApi {
//
// return new DataAndMac(enData, mac.getData());
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@Override
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, String mac) {
FutureTask<DataAndMacCheck> task = builder.getLongLinkWorks().fTask(new Callable<DataAndMacCheck>() {
@Override
public DataAndMacCheck call() throws Exception {
return builder.get().SYD_SM4_CheckMacAndDecrypt_ShortData(iEncKeyIndex, iMacKeyIndex, pcData, mac);
}
});
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] macs) {
try {
return task.get();
if ( ! ConnectHandlerManager.isInited() ) {
throw new SydApiException(-1);
}
byte[] decryptedData = new byte[pcData.length];
IntByReference decryptedDataLen = new IntByReference(decryptedData.length);
int i = SydLibrary.INSTANCE.SYD_SM4_Decrypt_Mac_ShortData(
ConnectHandlerManager.getHandler(),
iEncKeyIndex,
iMacKeyIndex,
pcData,
pcData.length,
macs,
decryptedData,
decryptedDataLen
);
byte[] data = new byte[decryptedDataLen.getValue()];
System.arraycopy(decryptedData, 0, data, 0, data.length);
System.out.println("outlen=" + data.length);
if ( 0 == i ){
return new DataAndMacCheck(data, true);
} if (0x01000010 == i) {
return new DataAndMacCheck(data, false);
} else {
throw new SydApiException(i);
}
} catch (Exception e) {
if ( e instanceof SydApiException) {
throw e;
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
// FutureTask<DataAndMacCheck> task = builder.getLongLinkWorks().fTask(new Callable<DataAndMacCheck>() {
// @Override
// public DataAndMacCheck call() throws Exception {
// return builder.get().SYD_SM4_CheckMacAndDecrypt_ShortData(iEncKeyIndex, iMacKeyIndex, pcData, mac);
// }
// });
// try {
// return task.get();
// } catch (Exception e) {
// throw new SydApiException("并行任务执行失败", 0xC101, e);
// }
}
@Override
public List<DataAndMac> SYD_SM4_EncryptAndMac_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<byte[]> pcData) {
FutureTask<List<DataAndMac>> task = builder.getLongLinkWorks().fTask(new Callable<List<DataAndMac>>() {
@Override
public List<DataAndMac> call() throws Exception {
return builder.get().SYD_SM4_EncryptAndMac_BatchData(iEncKeyIndex, iMacKeyIndex, pcData);
}
});
if ( ! ConnectHandlerManager.isInited() ) {
throw new SydApiException(-1);
}
try {
return task.get();
int size = pcData.size();
// 准备输入参数 - 直接使用 byte[][]
byte[][] pcDataArray = pcData.toArray(new byte[0][]);
int[] iDataLen = pcData.stream()
.mapToInt(bytes -> bytes.length)
.toArray();
// 准备输出缓冲区
byte[][] pcEncData = new byte[size][];
int[] iEncDataLen = new int[size];
for (int i = 0; i < size; i++) {
int maxEncLen = ((pcData.get(i).length + 15) / 16 * 16);
pcEncData[i] = new byte[maxEncLen];
iEncDataLen[i] = maxEncLen;
}
byte[] pcMac = new byte[16];
// 调用加密函数
int result = SydLibrary.INSTANCE.SYD_SM4_Encrypt_Mac_BatchData(
ConnectHandlerManager.getHandler(),
iEncKeyIndex,
iMacKeyIndex,
pcDataArray,
iDataLen,
size,
pcEncData,
iEncDataLen,
pcMac);
if (0 == result) {
// 转换结果为Java风格的对象列表
List<DataAndMac> results = new ArrayList<>();
for (int i = 0; i < size; i++) {
results.add(new DataAndMac(pcEncData[i], pcMac));
}
return results;
} else {
throw new SydApiException(result);
}
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
// FutureTask<List<DataAndMac>> task = builder.getLongLinkWorks().fTask(new Callable<List<DataAndMac>>() {
// @Override
// public List<DataAndMac> call() throws Exception {
// return builder.get().SYD_SM4_EncryptAndMac_BatchData(iEncKeyIndex, iMacKeyIndex, pcData);
// }
// });
// try {
// return task.get();
// } catch (Exception e) {
// throw new SydApiException("并行任务执行失败", 0xC101, e);
// }
}
@Override
public List<DataAndMacCheck> SYD_SM4_CheckMacAndDecrypt_BatchData(int[] iEncKeyIndex, int[] iMacKeyIndex, List<DataAndMac> pcData) {
FutureTask<List<DataAndMacCheck>> task = builder.getLongLinkWorks().fTask(new Callable<List<DataAndMacCheck>>() {
@Override
public List<DataAndMacCheck> call() throws Exception {
return builder.get().SYD_SM4_CheckMacAndDecrypt_BatchData(iEncKeyIndex, iMacKeyIndex, pcData);
}
});
try {
return task.get();
} catch (Exception e) {
throw new SydApiException("并行任务执行失败", 0xC101, e);
int size = pcData.size();
// 准备输入参数
byte[][] pcEncData = pcData.stream()
.map(DataAndMac::getData)
.toArray(byte[][]::new);
int[] iEncDataLen = pcData.stream()
.mapToInt(d -> d.getData().length)
.toArray();
byte[][] pcMac = pcData.stream()
.map(d -> d.getMac())
.toArray(byte[][]::new);
// 准备输出缓冲区
byte[][] pcOutData = new byte[size][];
int[] iOutDataLen = new int[size];
for (int i = 0; i < size; i++) {
int maxOutLen = pcData.get(i).getData().length;
pcOutData[i] = new byte[maxOutLen];
iOutDataLen[i] = maxOutLen;
}
int[] iCheckRet = new int[size];
// 调用解密函数
int result = SydLibrary.INSTANCE.SYD_SM4_Decrypt_Mac_BatchData(
ConnectHandlerManager.getHandler(),
iEncKeyIndex,
iMacKeyIndex,
pcEncData,
iEncDataLen,
pcMac,
size,
pcOutData,
iOutDataLen,
iCheckRet);
if (0 == result) {
// 转换结果为Java风格的对象列表
List<DataAndMacCheck> results = new ArrayList<>();
for (int i = 0; i < size; i++) {
results.add(new DataAndMacCheck(pcOutData[i], iCheckRet[i] == 0));
}
return results;
} else {
throw new SydApiException(result);
}
// FutureTask<List<DataAndMacCheck>> task = builder.getLongLinkWorks().fTask(new Callable<List<DataAndMacCheck>>() {
// @Override
// public List<DataAndMacCheck> call() throws Exception {
// return builder.get().SYD_SM4_CheckMacAndDecrypt_BatchData(iEncKeyIndex, iMacKeyIndex, pcData);
// }
// });
// try {
// return task.get();
// } catch (Exception e) {
// throw new SydApiException("并行任务执行失败", 0xC101, e);
// }
}
@Override
@@ -395,7 +609,7 @@ public class SydApiLongLinkWrap implements SydApi {
}
@Override
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
public List<byte[]> SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
if ( null == iKeyIndex || null == pcData ) {
throw new IllegalArgumentException("批量输入数据为 null");
}
@@ -405,7 +619,7 @@ public class SydApiLongLinkWrap implements SydApi {
}
if ( 0 == iKeyIndex.length ) {
return new String[0];
return new ArrayList<>();
}
// 将数据进行分组,防止超长。
List<List<byte[]>> dataGroups = new ArrayList<>();
@@ -442,13 +656,13 @@ public class SydApiLongLinkWrap implements SydApi {
keyGroups.add(curKeyGroup);
// 并发处理
List<FutureTask<String[]>> tasks = new ArrayList<>();
List<FutureTask<List<byte[]>>> tasks = new ArrayList<>();
for (int i = 0; i < dataGroups.size(); i++) {
int finalI = i;
FutureTask<String[]> task = builder.getLongLinkWorks().fTask(new Callable<String[]>() {
FutureTask<List<byte[]>> task = builder.getLongLinkWorks().fTask(new Callable<List<byte[]>>() {
@Override
public String[] call() throws Exception {
public List<byte[]> call() throws Exception {
SydApi api = builder.get();
return api.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI));
}
@@ -457,20 +671,23 @@ public class SydApiLongLinkWrap implements SydApi {
}
try {
List<String> ret = new ArrayList<>();
for (FutureTask<String[]> t : tasks) {
String[] macs = t.get();
ret.addAll(Arrays.asList(macs));
List<byte[]> ret = new ArrayList<>();
for (FutureTask<List<byte[]>> t : tasks) {
List<byte[]> macs = t.get();
ret.addAll(macs);
}
return ret.toArray(new String[0]);
return ret;
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}
@Override
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, List<byte[]> pcMac) {
if ( null == iKeyIndex || null == pcData ) {
throw new IllegalArgumentException("批量输入数据为 null");
}
@@ -486,12 +703,12 @@ public class SydApiLongLinkWrap implements SydApi {
// 将数据进行分组,防止超长。
List<List<byte[]>> dataGroups = new ArrayList<>();
List<List<Integer>> keyGroups = new ArrayList<>();
List<List<String>> macGroups = new ArrayList<>();
List<List<byte[]>> macGroups = new ArrayList<>();
int totalLen = 0;
List<byte[]> curDataGroup = new ArrayList<>();
List<Integer> curKeyGroup = new ArrayList<>();
List<String> curMacGroup = new ArrayList<>();
List<byte[]> curMacGroup = new ArrayList<>();
for (int i = 0; i < pcData.size(); i++) {
byte[] data = pcData.get(i);
@@ -516,7 +733,7 @@ public class SydApiLongLinkWrap implements SydApi {
curDataGroup.add(data);
curKeyGroup.add(iKeyIndex[i]);
curMacGroup.add(pcMac[i]);
curMacGroup.add(pcMac.get(i));
totalLen += data.length;
}
@@ -536,7 +753,7 @@ public class SydApiLongLinkWrap implements SydApi {
return api.SYD_SM4Mac_BatchData(
toIntArray(keyGroups.get(finalI)),
dataGroups.get(finalI),
macGroups.get(finalI).toArray(new String[0])
macGroups.get(finalI)
);
}
});
@@ -554,6 +771,9 @@ public class SydApiLongLinkWrap implements SydApi {
return toBoolArray(ret);
} catch (Exception e) {
if ( e.getCause() instanceof SydApiException) {
throw (SydApiException) e.getCause();
}
throw new SydApiException("并行任务执行失败", 0xC101, e);
}
}