MAC 统一为 bytes
This commit is contained in:
@@ -2,9 +2,9 @@ package com.sunyard.sge.database;
|
||||
|
||||
public class DataAndMac {
|
||||
private byte[] data;
|
||||
private String mac;
|
||||
private byte[] mac;
|
||||
|
||||
public DataAndMac(byte[] data, String mac) {
|
||||
public DataAndMac(byte[] data, byte[] mac) {
|
||||
this.data = data;
|
||||
this.mac = mac;
|
||||
}
|
||||
@@ -17,11 +17,11 @@ public class DataAndMac {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getMac() {
|
||||
public byte[] getMac() {
|
||||
return mac;
|
||||
}
|
||||
|
||||
public void setMac(String mac) {
|
||||
public void setMac(byte[] mac) {
|
||||
this.mac = mac;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,10 +177,7 @@ public interface SydApi {
|
||||
* @return String[] 数组,长度等于 pcData List 的长度。
|
||||
* String[] 数组中的每个值为 mac值(固定长度16字节)的 HEX字符串。
|
||||
*/
|
||||
String[] SYD_SM4Mac_BatchData(
|
||||
int[] iKeyIndex,
|
||||
List<byte[]> pcData
|
||||
);
|
||||
public List<byte[]> SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData);
|
||||
|
||||
|
||||
/**
|
||||
@@ -195,7 +192,7 @@ public interface SydApi {
|
||||
boolean[] SYD_SM4Mac_BatchData(
|
||||
int[] iKeyIndex,
|
||||
List<byte[]> pcData,
|
||||
String[] pcMac
|
||||
List<byte[]> pcMac
|
||||
);
|
||||
|
||||
|
||||
@@ -251,7 +248,7 @@ public interface SydApi {
|
||||
* @param pcData: 输入。计算mac所需要的数据。
|
||||
* @return mac值(固定长度16字节)转HEX 字符串格式
|
||||
*/
|
||||
String SYD_SM4Mac_ShortData(
|
||||
byte[] SYD_SM4Mac_ShortData(
|
||||
int iKeyIndex,
|
||||
byte[] pcData
|
||||
);
|
||||
@@ -268,7 +265,7 @@ public interface SydApi {
|
||||
boolean SYD_SM4Mac_ShortData(
|
||||
int iKeyIndex,
|
||||
byte[] pcData,
|
||||
String pcMac
|
||||
byte[] pcMac
|
||||
);
|
||||
|
||||
|
||||
@@ -300,7 +297,7 @@ public interface SydApi {
|
||||
int iEncKeyIndex,
|
||||
int iMacKeyIndex,
|
||||
byte[] pcData,
|
||||
String mac
|
||||
byte[] mac
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ public class SydApi4Database extends SydApiBaseFunction {
|
||||
}
|
||||
|
||||
@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");
|
||||
@@ -94,7 +94,7 @@ public class SydApi4Database extends SydApiBaseFunction {
|
||||
}
|
||||
|
||||
if ( 0 == iKeyIndex.length ) {
|
||||
return new String[0];
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 将数据进行分组,防止超长。
|
||||
@@ -132,16 +132,16 @@ public class SydApi4Database extends SydApiBaseFunction {
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 排队处理
|
||||
List<String> ret = new ArrayList<>();
|
||||
List<byte[]> 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));
|
||||
List<byte[]> macs = super.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(i)), dataGroups.get(i));
|
||||
ret.addAll(macs);
|
||||
}
|
||||
return ret.toArray(new String[0]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@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");
|
||||
@@ -158,12 +158,12 @@ public class SydApi4Database extends SydApiBaseFunction {
|
||||
// 将数据进行分组,防止超长。
|
||||
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);
|
||||
@@ -188,7 +188,7 @@ public class SydApi4Database extends SydApiBaseFunction {
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
curMacGroup.add(pcMac[i]);
|
||||
curMacGroup.add(pcMac.get(i));
|
||||
totalLen += data.length;
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ public class SydApi4Database extends SydApiBaseFunction {
|
||||
boolean[] rets = super.SYD_SM4Mac_BatchData(
|
||||
toIntArray(keyGroups.get(i)),
|
||||
dataGroups.get(i),
|
||||
macGroups.get(i).toArray(new String[0])
|
||||
macGroups.get(i)
|
||||
);
|
||||
for (boolean r : rets) {
|
||||
ret.add(r);
|
||||
|
||||
@@ -515,14 +515,14 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||
public List<byte[]> SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_BatchData:iKeyIndex:{},pcData元素个数为:{}", iKeyIndex, pcData.size());
|
||||
}
|
||||
|
||||
if (pcData.isEmpty()) {
|
||||
return new String[0];
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
@@ -545,12 +545,12 @@ public class SydApiBaseFunction implements SydApi {
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
String[] ret = new String[list.size()];
|
||||
List<byte[]> ret = new ArrayList<>(list.size());
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
byte[] mac = new byte[16];
|
||||
byte[] buff = list.get(i);
|
||||
System.arraycopy(buff, buff.length - 16, mac, 0, 16);
|
||||
ret[i] = Util.bytes2HexString(mac);
|
||||
ret.add( mac );
|
||||
}
|
||||
return ret;
|
||||
|
||||
@@ -566,12 +566,12 @@ public class SydApiBaseFunction implements SydApi {
|
||||
synchronized (api) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
String[] ret = new String[list.size()];
|
||||
List<byte[]> ret = new ArrayList<>(list.size());
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
byte[] mac = new byte[16];
|
||||
byte[] buff = list.get(i);
|
||||
System.arraycopy(buff, buff.length - 16, mac, 0, 16);
|
||||
ret[i] = Util.bytes2HexString(mac);
|
||||
ret.add( mac );
|
||||
}
|
||||
return ret;
|
||||
|
||||
@@ -593,10 +593,10 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
@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) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_BatchData方法接收参数:iKeyIndex:{}, pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), Arrays.toString(pcMac));
|
||||
log.debug("SYD_SM4Mac_BatchData 方法接收参数:iKeyIndex:{}, pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), pcMac.size());
|
||||
}
|
||||
|
||||
if (pcData.isEmpty()) {
|
||||
@@ -626,7 +626,7 @@ public class SydApiBaseFunction implements SydApi {
|
||||
byte[] mac = new byte[16];
|
||||
byte[] buff = list.get(i);
|
||||
System.arraycopy(buff, buff.length - 16, mac, 0, 16);
|
||||
ret[i] = pcMac[i].equals(Util.bytes2HexString(mac));
|
||||
ret[i] = Arrays.equals(pcMac.get(i), mac);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -647,7 +647,7 @@ public class SydApiBaseFunction implements SydApi {
|
||||
byte[] mac = new byte[16];
|
||||
byte[] buff = list.get(i);
|
||||
System.arraycopy(buff, buff.length - 16, mac, 0, 16);
|
||||
ret[i] = pcMac[i].equals(Util.bytes2HexString(mac));
|
||||
ret[i] = Arrays.equals(pcMac.get(i), mac);
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -777,11 +777,11 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
public byte[] SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
return SYD_SM4Mac_ShortData(iKeyIndex, pcData, 1024);
|
||||
}
|
||||
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen) {
|
||||
public byte[] SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData));
|
||||
@@ -843,15 +843,15 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, byte[] pcMac) {
|
||||
return SYD_SM4Mac_ShortData(iKeyIndex, pcData, 1024, pcMac);
|
||||
}
|
||||
|
||||
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen, String pcMac) {
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, int limitDataLen, byte[] pcMac) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:{}", iKeyIndex, Arrays.toString(pcData), pcMac);
|
||||
log.debug("SYD_SM4Mac_ShortData 方法接收参数:{}", iKeyIndex, Util.bytes2HexString(pcData), Util.bytes2HexString(pcMac));
|
||||
}
|
||||
|
||||
if ( null == pcData || pcData.length > limitDataLen ) {
|
||||
@@ -866,7 +866,7 @@ public class SydApiBaseFunction implements SydApi {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
updateLastUseTime();
|
||||
String mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData, limitDataLen);
|
||||
byte[] mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData, limitDataLen);
|
||||
log.debug(this + ".SYD_SM4Mac_ShortData方法返回值:{}", mac);
|
||||
return pcMac.equals(mac);
|
||||
}
|
||||
@@ -888,14 +888,14 @@ public class SydApiBaseFunction implements SydApi {
|
||||
// 参数检查在函数内部
|
||||
byte[] enData = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
// mac 长度放宽到 1024,因为密文填充
|
||||
String mac = SYD_SM4Mac_ShortData(iMacKeyIndex, enData, 1040);
|
||||
byte[] mac = SYD_SM4Mac_ShortData(iMacKeyIndex, enData, 1040);
|
||||
return new DataAndMac(enData, mac);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, String mac) {
|
||||
public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] mac) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_CheckMacAndDecrypt_ShortData:k1={} k2={}", iEncKeyIndex, iMacKeyIndex);
|
||||
@@ -933,10 +933,10 @@ public class SydApiBaseFunction implements SydApi {
|
||||
}
|
||||
|
||||
List<byte[]> enDatas = SYD_SM4_BatchData(iEncKeyIndex, pcData, Consts.ECB_ENC);
|
||||
String[] macs = SYD_SM4Mac_BatchData(iMacKeyIndex, enDatas);
|
||||
List<byte[]> macs = SYD_SM4Mac_BatchData(iMacKeyIndex, enDatas);
|
||||
List<DataAndMac> ret = new ArrayList<DataAndMac>();
|
||||
for ( int i=0 ; i< enDatas.size() ; i++ ) {
|
||||
DataAndMac dam = new DataAndMac(enDatas.get(i), macs[i]);
|
||||
DataAndMac dam = new DataAndMac(enDatas.get(i), macs.get(i));
|
||||
ret.add(dam);
|
||||
}
|
||||
return ret;
|
||||
@@ -961,10 +961,10 @@ public class SydApiBaseFunction implements SydApi {
|
||||
throw new SydApiException("密钥长度和数据长度不对应", 0x01000001);
|
||||
}
|
||||
|
||||
String[] macs = new String[pcData.size()];
|
||||
List<byte[]> macs = new ArrayList<>(pcData.size());
|
||||
List<byte[]>datas = new ArrayList<>(pcData.size());
|
||||
for ( int i=0 ; i<macs.length; i++ ) {
|
||||
macs[i] = pcData.get(i).getMac();
|
||||
for ( int i=0 ; i<macs.size(); i++ ) {
|
||||
macs.add(pcData.get(i).getMac());
|
||||
datas.add(pcData.get(i).getData());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.database.pool.SydApiPool;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.plus.ConnectHandlerManager;
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
import com.sunyard.sge.pool.PoolConfig;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -37,6 +39,14 @@ public class SydApiBuilder {
|
||||
log.debug("pool config {}", config);
|
||||
}
|
||||
|
||||
// 同时初始化 jna 下 c 的链接
|
||||
int ret = ConnectHandlerManager.init(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
||||
System.out.println("ConnectHandler return " + ret);
|
||||
if ( 0 != ret ) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
|
||||
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
||||
this.pool = new SydApiPool(config, linkInfo);
|
||||
longLinkWorks = new CommWorker();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user