Merge branch 'dev-cheney' into dev-1.0
# Conflicts: # pom.xml # src/main/java/com/sunyard/sge/database/SydApi4Database.java # src/main/java/com/sunyard/sge/database/SydApiBuilder.java # src/main/java/com/sunyard/sge/database/pool/SydApiPool.java # src/main/java/com/sunyard/sge/pool/ThreadBasedConnectPool.java # src/test/java/test/FuncTest.java # sydapi-database.iml
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public interface ISM3 {
|
||||
public void update(byte[] pcData);
|
||||
public String digest();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public interface ISM4 {
|
||||
public byte[] update(byte[] pcData);
|
||||
public byte[] finish();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public interface ISM4Mac {
|
||||
public void update(byte[] pcData);
|
||||
public String disget();
|
||||
}
|
||||
@@ -266,9 +266,9 @@ public interface SydApi {
|
||||
);
|
||||
|
||||
|
||||
public SydApiSM4 initSM4(int iKeyIndex, int ende);
|
||||
public ISM4 initSM4(int iKeyIndex, int ende);
|
||||
|
||||
public SydApiSM3 initSM3();
|
||||
public ISM3 initSM3();
|
||||
|
||||
public SydSM4Mac initSM4Mac(int iKeyIndex);
|
||||
public ISM4Mac initSM4Mac(int iKeyIndex);
|
||||
}
|
||||
|
||||
@@ -1,718 +1,176 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.pool.ObjectInPool;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
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 implements SydApi {
|
||||
|
||||
|
||||
private SydApi4j[] hsms = null;
|
||||
private HsmLinkInfo[] linkInfos = null;
|
||||
private ObjectInPool oip = null;
|
||||
|
||||
private boolean isShortLinkMode = false;
|
||||
public class SydApi4Database extends SydApiBaseFunction {
|
||||
|
||||
public SydApi4Database() {
|
||||
isShortLinkMode = true;
|
||||
super();
|
||||
}
|
||||
|
||||
public SydApi4Database(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
|
||||
this.hsms = hsms;
|
||||
this.linkInfos = linkInfos;
|
||||
super(hsms, linkInfos);
|
||||
}
|
||||
|
||||
public SydApi4j[] getHsms() {
|
||||
return hsms;
|
||||
}
|
||||
|
||||
public HsmLinkInfo[] getLinkInfos() {
|
||||
return linkInfos;
|
||||
}
|
||||
|
||||
public void setOip(ObjectInPool oip) {
|
||||
this.oip = oip;
|
||||
}
|
||||
|
||||
private void updateLastUseTime(){
|
||||
if ( null != this.oip ) {
|
||||
this.oip.updateLastUseTime();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
|
||||
if (pcIpList.length != iPortList.length) {
|
||||
throw new IllegalArgumentException("ip与端口数量不一致");
|
||||
}
|
||||
SydApi4j[] apis = new SydApi4j[pcIpList.length];
|
||||
int iDealTimeOut = allDealTimeOut / pcIpList.length;
|
||||
boolean connected = false;
|
||||
for (int i = 0; i < pcIpList.length; i++) {
|
||||
|
||||
try {
|
||||
|
||||
apis[i] = (SydApi4j) new SydApi4j().connect(pcIpList[i], iPortList[i], null, iConnectTimeOut, iDealTimeOut);
|
||||
connected = true;
|
||||
} catch (Exception e) {
|
||||
// 忽略错误
|
||||
}
|
||||
|
||||
}
|
||||
if (!connected) {
|
||||
// TODO
|
||||
throw new SydApiException("连接错误", -1);
|
||||
}
|
||||
|
||||
|
||||
hsms = apis;
|
||||
isShortLinkMode = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void SYD_Disconnect() {
|
||||
if (null == hsms) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for (int i = 0; i < hsms.length; i++) {
|
||||
SydApi4j api = hsms[i];
|
||||
if (null != api) {
|
||||
try {
|
||||
api.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
hsms = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SYD_Disconnect_Ex() {
|
||||
|
||||
if (isShortLinkMode) {
|
||||
SYD_Disconnect();
|
||||
}
|
||||
|
||||
// 配合链接池,不再需要释放。
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
SydApiException le = new SydApiException(-1);
|
||||
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||
return Util.bytes2HexString(hash);
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||
return Util.bytes2HexString(hash);
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
byte[] procData = null;
|
||||
if (2 == iPkgNum || 3 == iPkgNum) {
|
||||
procData = Util.hexString2Bytes(pcProcData);
|
||||
|
||||
}
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
||||
if (0 == iPkgNum || 3 == iPkgNum) {
|
||||
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
||||
} else {
|
||||
return new LongDataReturn<String, String>(Util.bytes2HexString(hash), null);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
||||
if (0 == iPkgNum || 3 == iPkgNum) {
|
||||
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
||||
} else {
|
||||
return new LongDataReturn<String, String>(Util.bytes2HexString(hash), null);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||
if (iFlag == Consts.ECB_DEC && null == pcData || pcData.length == 0) {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
updateLastUseTime();
|
||||
String keyIndex = String.format("%08d", iKeyIndex);
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
} else {
|
||||
return api.SYMDecryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
} else {
|
||||
return api.SYMDecryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||
// 将数据进行分组,防止超长。
|
||||
List<List<byte[]>> dataGroups = new ArrayList<>();
|
||||
List<List<Integer>> keyGroups = new ArrayList<>();
|
||||
int totalLen = 0;
|
||||
|
||||
if (iFlag == Consts.ECB_DEC) {
|
||||
for (byte[] pcdata : pcData) {
|
||||
if (null == pcData || pcdata.length == 0) {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
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 (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
if (
|
||||
curDataGroup.size() > 9 // 分组小于等于 9 个
|
||||
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
|
||||
) { // 扩展包
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
String[] keys = new String[iKeyIndex.length];
|
||||
updateLastUseTime();
|
||||
for (int i = 0; i < iKeyIndex.length; i++) {
|
||||
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
|
||||
}
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
curDataGroup = new ArrayList<>();
|
||||
curKeyGroup = new ArrayList<>();
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
||||
|
||||
} else {
|
||||
return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
||||
|
||||
} else {
|
||||
return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||
totalLen = data.length;
|
||||
|
||||
if (iFlag == Consts.ECB_DEC && (null == pcData || pcData.length == 0)) {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
updateLastUseTime();
|
||||
String keyIndex = String.format("%08d", iKeyIndex);
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYD_SM4_LongData(
|
||||
8,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
} else {
|
||||
return api.SYD_SM4_LongData(
|
||||
9,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYD_SM4_LongData(
|
||||
8,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
} else {
|
||||
return api.SYD_SM4_LongData(
|
||||
9,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
if (null==e){
|
||||
|
||||
}
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw le;
|
||||
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) {
|
||||
// 将数据进行分组,防止超长。
|
||||
List<List<byte[]>> dataGroups = new ArrayList<>();
|
||||
List<List<Integer>> keyGroups = new ArrayList<>();
|
||||
int totalLen = 0;
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
List<byte[]> curDataGroup = new ArrayList<>();
|
||||
List<Integer> curKeyGroup = new ArrayList<>();
|
||||
|
||||
String[] keys = new String[iKeyIndex.length];
|
||||
updateLastUseTime();
|
||||
for (int i = 0; i < iKeyIndex.length; i++) {
|
||||
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
|
||||
}
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
for (int i = 0; i < pcData.size(); i++) {
|
||||
byte[] data = pcData.get(i);
|
||||
if (null == data) {
|
||||
data = new byte[0];
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
String[] ret = new String[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(buff);
|
||||
}
|
||||
return ret;
|
||||
if (
|
||||
curDataGroup.size() > 9 // 分组小于等于 9 个
|
||||
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
|
||||
) {// 扩展包
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
String[] ret = new String[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(buff);
|
||||
}
|
||||
return ret;
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
curDataGroup = new ArrayList<>();
|
||||
curKeyGroup = new ArrayList<>();
|
||||
}
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
|
||||
totalLen = data.length;
|
||||
}
|
||||
throw le;
|
||||
|
||||
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) {
|
||||
// 将数据进行分组,防止超长。
|
||||
List<List<byte[]>> dataGroups = new ArrayList<>();
|
||||
List<List<Integer>> keyGroups = new ArrayList<>();
|
||||
List<List<String>> macGroups = new ArrayList<>();
|
||||
int totalLen = 0;
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
List<byte[]> curDataGroup = new ArrayList<>();
|
||||
List<Integer> curKeyGroup = new ArrayList<>();
|
||||
List<String> curMacGroup = new ArrayList<>();
|
||||
|
||||
String[] keys = new String[iKeyIndex.length];
|
||||
updateLastUseTime();
|
||||
for (int i = 0; i < iKeyIndex.length; i++) {
|
||||
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
|
||||
}
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
for (int i = 0; i < pcData.size(); i++) {
|
||||
byte[] data = pcData.get(i);
|
||||
if (null == data) {
|
||||
data = new byte[0];
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
boolean[] ret = new boolean[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] = pcMac[i].equals(Util.bytes2HexString(buff));
|
||||
}
|
||||
if (
|
||||
curDataGroup.size() > 9 // 分组小于等于 9 个
|
||||
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
|
||||
) {
|
||||
// 扩展包
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
macGroups.add(curMacGroup);
|
||||
|
||||
return ret;
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
boolean[] ret = new boolean[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] = pcMac[i].equals(Util.bytes2HexString(buff));
|
||||
}
|
||||
curDataGroup = new ArrayList<>();
|
||||
curKeyGroup = new ArrayList<>();
|
||||
curMacGroup = new ArrayList<>();
|
||||
}
|
||||
|
||||
return ret;
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
return toBoolArray(ret);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
String keyIndex = String.format("%08d", iKeyIndex);
|
||||
|
||||
byte[] iv = null;
|
||||
|
||||
// 仅一包 和 第一包 忽略 pcProcData 参数
|
||||
if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) {
|
||||
iv = pcProcData.getBytes();
|
||||
}
|
||||
SydApiException le = new SydApiException(-1);
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] ret = api.SYD_SM4_LongData(
|
||||
8,
|
||||
2, iv,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
|
||||
byte[] mac = new byte[16];
|
||||
System.arraycopy(ret, ret.length - 16, mac, 0, 16);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) {
|
||||
return new LongDataReturn<>(null, Util.bytes2HexString(mac));
|
||||
} else {
|
||||
// 返回中间值
|
||||
return new LongDataReturn<>(Util.bytes2HexString(mac), null);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
byte[] ret = api.SYD_SM4_LongData(
|
||||
8,
|
||||
2, iv,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
|
||||
byte[] mac = new byte[16];
|
||||
System.arraycopy(ret, ret.length - 16, mac, 0, 16);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) {
|
||||
return new LongDataReturn<>(null, Util.bytes2HexString(mac));
|
||||
} else {
|
||||
// 返回中间值
|
||||
return new LongDataReturn<>(Util.bytes2HexString(mac), null);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) {
|
||||
|
||||
updateLastUseTime();
|
||||
LongDataReturn<String, String> ret = SYD_SM4Mac_LongData(iKeyIndex, pcData, iPkgNum, pcProcData);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) {
|
||||
return new LongDataReturn<>(null, pcMac.equals(ret.getData()));
|
||||
} else {
|
||||
// 返回中间值
|
||||
return new LongDataReturn<>(ret.getProcData(), false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
|
||||
return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
else {
|
||||
synchronized (api){
|
||||
try {
|
||||
|
||||
return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
if (null == pcMac) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
updateLastUseTime();
|
||||
String mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData);
|
||||
return pcMac.equals(mac);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public SydApiSM4 initSM4(int iKeyIndex, int ende) {
|
||||
|
||||
return new SydApiSM4(this,iKeyIndex,ende);
|
||||
}
|
||||
public SydApiSM3 initSM3() {
|
||||
|
||||
return new SydApiSM3(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydSM4Mac initSM4Mac(int iKeyIndex) {
|
||||
return new SydSM4Mac(this, iKeyIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,809 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.pool.ObjectInPool;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 上海黄金交易所 - 数据库加密方案
|
||||
*/
|
||||
public class SydApiBaseFunction implements SydApi {
|
||||
|
||||
private SydApi4j[] hsms = null;
|
||||
private HsmLinkInfo[] linkInfos = null;
|
||||
private ObjectInPool oip = null;
|
||||
|
||||
private boolean isShortLinkMode = false;
|
||||
|
||||
public SydApiBaseFunction() {
|
||||
isShortLinkMode = true;
|
||||
}
|
||||
|
||||
public SydApiBaseFunction(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
|
||||
this.hsms = hsms;
|
||||
this.linkInfos = linkInfos;
|
||||
}
|
||||
|
||||
public SydApi4j[] getHsms() {
|
||||
return hsms;
|
||||
}
|
||||
|
||||
public HsmLinkInfo[] getLinkInfos() {
|
||||
return linkInfos;
|
||||
}
|
||||
|
||||
public void setOip(ObjectInPool oip) {
|
||||
this.oip = oip;
|
||||
}
|
||||
|
||||
private void updateLastUseTime(){
|
||||
if ( null != this.oip ) {
|
||||
this.oip.updateLastUseTime();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, allDealTimeOut);
|
||||
}
|
||||
|
||||
if (pcIpList.length != iPortList.length) {
|
||||
throw new IllegalArgumentException("ip与端口数量不一致");
|
||||
}
|
||||
SydApi4j[] apis = new SydApi4j[pcIpList.length];
|
||||
int iDealTimeOut = allDealTimeOut / pcIpList.length;
|
||||
boolean connected = false;
|
||||
for (int i = 0; i < pcIpList.length; i++) {
|
||||
|
||||
try {
|
||||
|
||||
apis[i] = (SydApi4j) new SydApi4j().connect(pcIpList[i], iPortList[i], null, iConnectTimeOut, iDealTimeOut);
|
||||
connected = true;
|
||||
} catch (Exception e) {
|
||||
// 忽略错误
|
||||
log.error("网络连接错误 {} {} - 忽略", pcIpList[i], iPortList[i]);
|
||||
}
|
||||
|
||||
}
|
||||
if (!connected) {
|
||||
throw new SydApiException("连接错误", -1);
|
||||
}
|
||||
|
||||
|
||||
hsms = apis;
|
||||
isShortLinkMode = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void SYD_Disconnect() {
|
||||
if (null == hsms) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for (int i = 0; i < hsms.length; i++) {
|
||||
SydApi4j api = hsms[i];
|
||||
if (null != api) {
|
||||
try {
|
||||
api.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
hsms = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SYD_Disconnect_Ex() {
|
||||
|
||||
if (isShortLinkMode) {
|
||||
SYD_Disconnect();
|
||||
}
|
||||
|
||||
// 配合链接池,不再需要释放。
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM3_Hash_ShortData方法接收参数:pcData:{}", Arrays.toString(pcData));
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
SydApiException le = new SydApiException(-1);
|
||||
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||
return Util.bytes2HexString(hash);
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||
return Util.bytes2HexString(hash);
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM3_Hash_LongData方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||
}
|
||||
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
byte[] procData = null;
|
||||
if (2 == iPkgNum || 3 == iPkgNum) {
|
||||
procData = Util.hexString2Bytes(pcProcData);
|
||||
log.debug("Util.hexString2Bytes方法返回值:{}", Arrays.toString(procData));
|
||||
|
||||
}
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||
if (0 == iPkgNum || 3 == iPkgNum) {
|
||||
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
||||
} else {
|
||||
return new LongDataReturn<String, String>(Util.bytes2HexString(hash), null);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||
if (0 == iPkgNum || 3 == iPkgNum) {
|
||||
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
||||
} else {
|
||||
return new LongDataReturn<String, String>(Util.bytes2HexString(hash), null);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iFlag);
|
||||
}
|
||||
if (iFlag == Consts.ECB_DEC && null == pcData || pcData.length == 0) {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
updateLastUseTime();
|
||||
String keyIndex = String.format("%08d", iKeyIndex);
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
} else {
|
||||
return api.SYMDecryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
} else {
|
||||
return api.SYMDecryptData(
|
||||
com.sunyard.SydApi.NALG_SM4,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},iFlag:{}", iKeyIndex, pcData.size(), iFlag);
|
||||
}
|
||||
if (iFlag == Consts.ECB_DEC) {
|
||||
for (byte[] pcdata : pcData) {
|
||||
if (null == pcData || pcdata.length == 0) {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
String[] keys = new String[iKeyIndex.length];
|
||||
updateLastUseTime();
|
||||
for (int i = 0; i < iKeyIndex.length; i++) {
|
||||
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
|
||||
}
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
||||
|
||||
} else {
|
||||
return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
||||
|
||||
} else {
|
||||
return api.SYMEncryptDecryptBatchData(9, 1, null, keys, 1, pcData);
|
||||
}
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag);
|
||||
}
|
||||
if (iFlag == Consts.ECB_DEC && (null == pcData || pcData.length == 0)) {
|
||||
throw new SydApiException(19);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
updateLastUseTime();
|
||||
String keyIndex = String.format("%08d", iKeyIndex);
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYD_SM4_LongData(
|
||||
8,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
} else {
|
||||
return api.SYD_SM4_LongData(
|
||||
9,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
if (Consts.ECB_ENC == iFlag) {
|
||||
return api.SYD_SM4_LongData(
|
||||
8,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
} else {
|
||||
return api.SYD_SM4_LongData(
|
||||
9,
|
||||
1, null,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
if (null==e){
|
||||
|
||||
}
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] 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 (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
String[] keys = new String[iKeyIndex.length];
|
||||
updateLastUseTime();
|
||||
for (int i = 0; i < iKeyIndex.length; i++) {
|
||||
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
|
||||
}
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
String[] ret = new String[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(buff);
|
||||
}
|
||||
return ret;
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
String[] ret = new String[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(buff);
|
||||
}
|
||||
return ret;
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), Arrays.toString(pcMac));
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
String[] keys = new String[iKeyIndex.length];
|
||||
updateLastUseTime();
|
||||
for (int i = 0; i < iKeyIndex.length; i++) {
|
||||
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
|
||||
}
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
boolean[] ret = new boolean[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] = pcMac[i].equals(Util.bytes2HexString(buff));
|
||||
}
|
||||
|
||||
return ret;
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||
boolean[] ret = new boolean[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] = pcMac[i].equals(Util.bytes2HexString(buff));
|
||||
}
|
||||
|
||||
return ret;
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
String keyIndex = String.format("%08d", iKeyIndex);
|
||||
|
||||
byte[] iv = null;
|
||||
|
||||
// 仅一包 和 第一包 忽略 pcProcData 参数
|
||||
if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) {
|
||||
iv = pcProcData.getBytes();
|
||||
}
|
||||
SydApiException le = new SydApiException(-1);
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] ret = api.SYD_SM4_LongData(
|
||||
8,
|
||||
2, iv,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
|
||||
byte[] mac = new byte[16];
|
||||
System.arraycopy(ret, ret.length - 16, mac, 0, 16);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) {
|
||||
return new LongDataReturn<>(null, Util.bytes2HexString(mac));
|
||||
} else {
|
||||
// 返回中间值
|
||||
return new LongDataReturn<>(Util.bytes2HexString(mac), null);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
try {
|
||||
byte[] ret = api.SYD_SM4_LongData(
|
||||
8,
|
||||
2, iv,
|
||||
"SK" + SYMUtil.genPrivateKeyMark("000000", "00A", keyIndex),
|
||||
1,
|
||||
pcData, iPkgNum);
|
||||
|
||||
byte[] mac = new byte[16];
|
||||
System.arraycopy(ret, ret.length - 16, mac, 0, 16);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) {
|
||||
return new LongDataReturn<>(null, Util.bytes2HexString(mac));
|
||||
} else {
|
||||
// 返回中间值
|
||||
return new LongDataReturn<>(Util.bytes2HexString(mac), null);
|
||||
}
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData, pcMac);
|
||||
}
|
||||
updateLastUseTime();
|
||||
LongDataReturn<String, String> ret = SYD_SM4Mac_LongData(iKeyIndex, pcData, iPkgNum, pcProcData);
|
||||
|
||||
// 仅一包 和 尾包 返回最终结果
|
||||
if (Consts.PkgNumAll == iPkgNum || Consts.PkgNumTail == iPkgNum) {
|
||||
return new LongDataReturn<>(null, pcMac.equals(ret.getData()));
|
||||
} else {
|
||||
// 返回中间值
|
||||
return new LongDataReturn<>(ret.getProcData(), false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData));
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
|
||||
log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}", keyIndex);
|
||||
|
||||
SydApiException le = new SydApiException(-1);
|
||||
updateLastUseTime();
|
||||
for (SydApi4j api : this.hsms) {
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
|
||||
return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
else {
|
||||
synchronized (api){
|
||||
try {
|
||||
|
||||
return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
|
||||
|
||||
} catch (SydApiException e) {
|
||||
le = e;
|
||||
if (e.getRetCode() < 0) {
|
||||
log.error("HA 重试");
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw le;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
if (LogFactory.iLogLevel == 3) {
|
||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:{}", iKeyIndex, Arrays.toString(pcData), pcMac);
|
||||
}
|
||||
if (null == this.hsms || this.hsms.length < 1) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
|
||||
if (null == pcMac) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
updateLastUseTime();
|
||||
String mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData);
|
||||
log.debug(this + ".SYD_SM4Mac_ShortData方法返回值:{}", mac);
|
||||
return pcMac.equals(mac);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ISM4 initSM4(int iKeyIndex, int ende) {
|
||||
|
||||
return new SydSM4(this,iKeyIndex,ende);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydSM3 initSM3() {
|
||||
return new SydSM3(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydSM4Mac initSM4Mac(int iKeyIndex) {
|
||||
return new SydSM4Mac(this, iKeyIndex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected static int[] toIntArray(List<Integer> list) {
|
||||
if (null == list || 0 == list.size()) {
|
||||
return new int[0];
|
||||
}
|
||||
int[] ia = new int[list.size()];
|
||||
for (int i = 0; i < ia.length; i++) {
|
||||
ia[i] = list.get(i);
|
||||
}
|
||||
return ia;
|
||||
}
|
||||
|
||||
protected static boolean[] toBoolArray(List<Boolean> list) {
|
||||
if (null == list || 0 == list.size()) {
|
||||
return new boolean[0];
|
||||
}
|
||||
boolean[] ia = new boolean[list.size()];
|
||||
for (int i = 0; i < ia.length; i++) {
|
||||
ia[i] = list.get(i);
|
||||
}
|
||||
return ia;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.database.pool.SydApiPool;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
import com.sunyard.sge.pool.PoolConfig;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -11,21 +14,59 @@ public class SydApiBuilder {
|
||||
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||
}
|
||||
private SydApiPool pool;
|
||||
private CommWorker longLinkWorks;
|
||||
|
||||
|
||||
public SydApiBuilder(
|
||||
String[] pcIpList,
|
||||
int[] iPortList,
|
||||
int iConnectTimeOut,
|
||||
int iDealTimeOut
|
||||
) {
|
||||
this( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut, new PoolConfig() );
|
||||
}
|
||||
public SydApiBuilder(
|
||||
String[] pcIpList,
|
||||
int[] iPortList,
|
||||
int iConnectTimeOut,
|
||||
int iDealTimeOut,
|
||||
PoolConfig config
|
||||
) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
|
||||
if ( log.isDebugEnabled() ) {
|
||||
log.debug("创建链接");
|
||||
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
|
||||
log.debug("pool config {}", config);
|
||||
}
|
||||
|
||||
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
||||
this.pool = new SydApiPool(linkInfo);
|
||||
longLinkWorks = new CommWorker();
|
||||
warmUp( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
|
||||
}
|
||||
|
||||
public SydApi build() {
|
||||
public CommWorker getLongLinkWorks() {
|
||||
return longLinkWorks;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取池内对象
|
||||
* @return 池内对象
|
||||
*/
|
||||
public SydApi get(){
|
||||
return pool.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 升级
|
||||
* 修改了原实现
|
||||
* @return 包装对象
|
||||
*/
|
||||
public SydApi build() {
|
||||
return new SydApiLongLinkWrap( this );
|
||||
}
|
||||
|
||||
|
||||
private void warmUp(String[] pcIpList,
|
||||
int[] iPortList,
|
||||
|
||||
@@ -4,6 +4,9 @@ package com.sunyard.sge.database;
|
||||
* */
|
||||
public class SydApiConfig {
|
||||
|
||||
// 批量数据,每组最大长度
|
||||
public static final int BATCH_SIZE = 7680;
|
||||
|
||||
// 数据包长度
|
||||
private final int MaxDataLenEachPack = 4064;
|
||||
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
import static com.sunyard.sge.database.SydApiBaseFunction.toBoolArray;
|
||||
import static com.sunyard.sge.database.SydApiBaseFunction.toIntArray;
|
||||
import static com.sunyard.sge.database.SydApiConfig.BATCH_SIZE;
|
||||
|
||||
/**
|
||||
* 长连接的真正执行过程转发到长连接线程池中执行。
|
||||
*/
|
||||
public class SydApiLongLinkWrap implements SydApi {
|
||||
|
||||
private SydApiBuilder builder;
|
||||
|
||||
public SydApiLongLinkWrap(SydApiBuilder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iDealTimeOut) {
|
||||
throw new UnsupportedOperationException("此对象为长连接专用");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SYD_Disconnect_Ex() {
|
||||
// pass
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
||||
FutureTask<String> task = builder.getLongLinkWorks().fTask(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return builder.get().SYD_SM3_Hash_ShortData(pcData);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return task.get();
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
FutureTask<LongDataReturn<String, String>> task = builder.getLongLinkWorks().fTask(new Callable<LongDataReturn<String, String>>() {
|
||||
@Override
|
||||
public LongDataReturn<String, String> call() throws Exception {
|
||||
return builder.get().SYD_SM3_Hash_LongData(pcData, iPkgNum, pcProcData);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return task.get();
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||
FutureTask<byte[]> task = builder.getLongLinkWorks().fTask(new Callable<byte[]>() {
|
||||
@Override
|
||||
public byte[] call() throws Exception {
|
||||
return builder.get().SYD_SM4_ShortData(iKeyIndex, pcData, iFlag);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return task.get();
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISM4 initSM4(int iKeyIndex, int ende) {
|
||||
return new SydSM4(this, iKeyIndex, ende);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISM3 initSM3() {
|
||||
return new SydSM3(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ISM4Mac initSM4Mac(int iKeyIndex) {
|
||||
return new SydSM4Mac(this, iKeyIndex);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||
// 将数据进行分组,防止超长。
|
||||
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<>();
|
||||
}
|
||||
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
|
||||
totalLen = data.length;
|
||||
|
||||
}
|
||||
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<List<byte[]>>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<List<byte[]>> task = builder.getLongLinkWorks().fTask(new Callable<List<byte[]>>() {
|
||||
@Override
|
||||
public List<byte[]> call() throws Exception {
|
||||
SydApi api = builder.get();
|
||||
return api.SYD_SM4_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI), iFlag);
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<byte[]> ret = new ArrayList<>();
|
||||
for (FutureTask<List<byte[]>> t : tasks) {
|
||||
ret.addAll(t.get());
|
||||
}
|
||||
return ret;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||
// 将数据进行分组,防止超长。
|
||||
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<>();
|
||||
}
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
|
||||
totalLen = data.length;
|
||||
}
|
||||
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<String[]>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<String[]> task = builder.getLongLinkWorks().fTask(new Callable<String[]>() {
|
||||
@Override
|
||||
public String[] call() throws Exception {
|
||||
SydApi api = builder.get();
|
||||
return api.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI));
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> ret = new ArrayList<>();
|
||||
for (FutureTask<String[]> t : tasks) {
|
||||
String[] macs = t.get();
|
||||
ret.addAll(Arrays.asList(macs));
|
||||
}
|
||||
return ret.toArray(new String[0]);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
||||
// 将数据进行分组,防止超长。
|
||||
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<>();
|
||||
}
|
||||
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
curMacGroup.add(pcMac[i]);
|
||||
totalLen = data.length;
|
||||
}
|
||||
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
macGroups.add(curMacGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<boolean[]>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<boolean[]> task = builder.getLongLinkWorks().fTask(new Callable<boolean[]>() {
|
||||
@Override
|
||||
public boolean[] call() throws Exception {
|
||||
SydApi api = builder.get();
|
||||
return api.SYD_SM4Mac_BatchData(
|
||||
toIntArray(keyGroups.get(finalI)),
|
||||
dataGroups.get(finalI),
|
||||
macGroups.get(finalI).toArray(new String[0])
|
||||
);
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<Boolean> ret = new ArrayList<>();
|
||||
for (FutureTask<boolean[]> t : tasks) {
|
||||
boolean[] rets = t.get();
|
||||
for (boolean r : rets) {
|
||||
ret.add(r);
|
||||
}
|
||||
}
|
||||
return toBoolArray(ret);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-9
@@ -3,19 +3,12 @@ package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SydApiSM3 {
|
||||
public class SydSM3 implements ISM3 {
|
||||
private boolean is_finished = false;
|
||||
private SydApi api;
|
||||
private int iPkgNum;
|
||||
private String pcProcData;
|
||||
|
||||
public SydApiSM3(SydApi sydApi) {
|
||||
public SydSM3(SydApi sydApi) {
|
||||
this.api = sydApi;
|
||||
}
|
||||
|
||||
+3
-8
@@ -4,7 +4,7 @@ import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.bytes.BytesQueue;
|
||||
|
||||
|
||||
public class SydApiSM4 {
|
||||
public class SydSM4 implements ISM4 {
|
||||
|
||||
private SydApi api;
|
||||
private int iKeyIndex;
|
||||
@@ -13,10 +13,10 @@ public class SydApiSM4 {
|
||||
private int iFlag;
|
||||
private boolean is_finished = false;
|
||||
private byte[] dataResult = new byte[0];
|
||||
BytesQueue queue = new BytesQueue();
|
||||
private BytesQueue queue = new BytesQueue();
|
||||
|
||||
|
||||
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
|
||||
public SydSM4(SydApi api, int iKeyIndex, int endeFlag) {
|
||||
this.api = api;
|
||||
this.iFlag = endeFlag;
|
||||
this.iKeyIndex = iKeyIndex;
|
||||
@@ -66,9 +66,6 @@ public class SydApiSM4 {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (null == queue || null == pcData) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
queue.push(pcData);
|
||||
if ((queue.getLength()) / 16 < 2) {
|
||||
return new byte[0];
|
||||
@@ -78,9 +75,7 @@ public class SydApiSM4 {
|
||||
|
||||
iPkgNum = 2;
|
||||
return bytes2;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] finish() {
|
||||
+2
-2
@@ -5,7 +5,7 @@ import com.sunyard.proto.Util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SydApiSM4Backup {
|
||||
public class SydSM4Backup implements ISM4 {
|
||||
|
||||
private SydApi api;
|
||||
private int iKeyIndex;
|
||||
@@ -18,7 +18,7 @@ public class SydApiSM4Backup {
|
||||
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
||||
|
||||
|
||||
public SydApiSM4Backup(SydApi api, int iKeyIndex, int endeFlag) {
|
||||
public SydSM4Backup(SydApi api, int iKeyIndex, int endeFlag) {
|
||||
this.api = api;
|
||||
this.iFlag = endeFlag;
|
||||
this.iKeyIndex = iKeyIndex;
|
||||
@@ -5,31 +5,19 @@ import com.sunyard.sge.bytes.BytesQueue;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SydSM4Mac {
|
||||
private SydApi api;
|
||||
private byte[] last_in_data;
|
||||
private int iKeyIndex;
|
||||
public class SydSM4Mac implements ISM4Mac {
|
||||
private final SydApi api;
|
||||
private final int iKeyIndex;
|
||||
private int iPkgNum = 1;
|
||||
private String pcProcData;
|
||||
private boolean is_finished;
|
||||
private String pcMac;
|
||||
private byte[] pcdataResult = new byte[0];
|
||||
private String pcdataStr;
|
||||
BytesQueue queue = new BytesQueue();
|
||||
private BytesQueue queue = new BytesQueue();
|
||||
|
||||
public SydSM4Mac(SydApi api, int iKeyIndex) {
|
||||
this.api = api;
|
||||
this.iKeyIndex = iKeyIndex;
|
||||
this.pcProcData = pcProcData;
|
||||
// this.pcMac=pcMac;
|
||||
}
|
||||
|
||||
public SydSM4Mac(SydApi api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public void setPcMac(String pcMac) {
|
||||
this.pcMac = pcMac;
|
||||
this.pcProcData = null;
|
||||
}
|
||||
|
||||
public void update(byte[] pcData) {
|
||||
@@ -37,7 +25,7 @@ public class SydSM4Mac {
|
||||
return;
|
||||
}
|
||||
int length = pcData.length;
|
||||
this.pcdataResult = pcData;
|
||||
byte[] pcdataResult = pcData;
|
||||
if (is_finished) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@@ -51,7 +39,7 @@ public class SydSM4Mac {
|
||||
if (length % 16 != 0) {
|
||||
queue.push(pcData);
|
||||
byte[] pop = queue.pop(16, 0);
|
||||
pcdataResult=pop;
|
||||
pcdataResult =pop;
|
||||
}
|
||||
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData);
|
||||
@@ -82,7 +70,7 @@ public class SydSM4Mac {
|
||||
}
|
||||
|
||||
}
|
||||
public LongDataReturn disget() {
|
||||
public String disget() {
|
||||
|
||||
if (is_finished) {
|
||||
throw new SydApiException(0);
|
||||
@@ -95,13 +83,8 @@ public class SydSM4Mac {
|
||||
}
|
||||
byte[] pop = queue.pop();
|
||||
|
||||
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData);
|
||||
return stringStringLongDataReturn;
|
||||
} else {
|
||||
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData, pcMac);
|
||||
return stringBooleanLongDataReturn;
|
||||
}
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData);
|
||||
return stringStringLongDataReturn.getData();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.sunyard.sge.database.pool;
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.database.SydApi4Database;
|
||||
import com.sunyard.sge.database.SydApiBaseFunction;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.pool.ObjectInPool;
|
||||
import com.sunyard.sge.pool.ThreadBasedConnectPool;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -36,7 +38,9 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||
);
|
||||
connected = true;
|
||||
} catch (Exception e) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
// 忽略单个错误
|
||||
log.error("网络连接错误 {} {} - 忽略", info.getPcIp(), info.getiPort());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +49,7 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||
}
|
||||
|
||||
|
||||
return new SydApi4Database(apis, linkInfos);
|
||||
return new SydApiBaseFunction(apis, linkInfos);
|
||||
}
|
||||
|
||||
// 保活接口
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.sunyard.sge.pool;
|
||||
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* 负责真正的阻塞通信
|
||||
*/
|
||||
public class CommWorker {
|
||||
// 连接池配置
|
||||
private final PoolConfig poolConfig;
|
||||
// 真正的通信线程
|
||||
private final ExecutorService threadPool;
|
||||
|
||||
public CommWorker(){
|
||||
this( new PoolConfig() );
|
||||
}
|
||||
public CommWorker(PoolConfig poolConfig) {
|
||||
this.poolConfig = poolConfig;
|
||||
threadPool = new ThreadPoolExecutor(
|
||||
this.poolConfig.getKeepAliveNumber(),
|
||||
this.poolConfig.getMaxAliveNumber(),
|
||||
this.poolConfig.getMaxIdle(),
|
||||
TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行一个异步任务
|
||||
* @param callable
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public <T> FutureTask<T> fTask(Callable<T> callable) {
|
||||
FutureTask<T> task = new FutureTask<>(callable);
|
||||
threadPool.submit( task );
|
||||
return task;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,10 +3,16 @@ package com.sunyard.sge.pool;
|
||||
public class PoolConfig {
|
||||
|
||||
// 最大空闲时间
|
||||
private long maxIdle = 5L * 60L ; // 5 分钟
|
||||
private long maxIdle = 5L * 60 * 1000L ; // 5 分钟
|
||||
|
||||
// 最大无用时间
|
||||
private long maxUseless = 30L * 60L ; // 30 分钟
|
||||
private long maxUseless = 30L * 60 * 1000L ; // 30 分钟
|
||||
|
||||
// 最小保持连接数
|
||||
private int keepAliveNumber = 5;
|
||||
|
||||
// 最大连接数
|
||||
private int maxAliveNumber = 100;
|
||||
|
||||
public long getMaxIdle() {
|
||||
return maxIdle;
|
||||
@@ -23,4 +29,30 @@ public class PoolConfig {
|
||||
public void setMaxUseless(long maxUseless) {
|
||||
this.maxUseless = maxUseless;
|
||||
}
|
||||
|
||||
public int getKeepAliveNumber() {
|
||||
return keepAliveNumber;
|
||||
}
|
||||
|
||||
public void setKeepAliveNumber(int keepAliveNumber) {
|
||||
this.keepAliveNumber = keepAliveNumber;
|
||||
}
|
||||
|
||||
public int getMaxAliveNumber() {
|
||||
return maxAliveNumber;
|
||||
}
|
||||
|
||||
public void setMaxAliveNumber(int maxAliveNumber) {
|
||||
this.maxAliveNumber = maxAliveNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PoolConfig{" +
|
||||
"maxIdle=" + maxIdle +
|
||||
", maxUseless=" + maxUseless +
|
||||
", keepAliveNumber=" + keepAliveNumber +
|
||||
", maxAliveNumber=" + maxAliveNumber +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.sunyard.sge.pool;
|
||||
|
||||
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@@ -25,13 +27,30 @@ public class ThreadBasedConnectPool<T> {
|
||||
threadPool.scheduleAtFixedRate(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Logger log = LogFactory.getLogger();
|
||||
log.debug("连接池检测进程开始执行");
|
||||
log.debug("共有链接 {} 个", map.size());
|
||||
|
||||
// 检测和回收逻辑
|
||||
Iterator<Map.Entry<Thread, ObjectInPool<T>>> it = map.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry<Thread, ObjectInPool<T>> entry = it.next();
|
||||
|
||||
ObjectInPool<T> oip = entry.getValue();
|
||||
|
||||
if (Thread.State.TERMINATED == entry.getKey().getState()) {
|
||||
// 如果线程结束
|
||||
log.debug("回收一个链接,关联线程已释放。");
|
||||
|
||||
it.remove();
|
||||
|
||||
try {
|
||||
free(oip.getObject());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (System.currentTimeMillis() - oip.getLasUseAt() < config.getMaxIdle()) {
|
||||
// 空闲时间不足,免检。
|
||||
continue;
|
||||
@@ -39,6 +58,7 @@ public class ThreadBasedConnectPool<T> {
|
||||
|
||||
if (System.currentTimeMillis() - oip.getLasUseAt() > config.getMaxUseless()) {
|
||||
// 长时间未使用,回收链接
|
||||
log.debug("回收一个链接,超时。");
|
||||
|
||||
it.remove();
|
||||
|
||||
@@ -54,11 +74,11 @@ public class ThreadBasedConnectPool<T> {
|
||||
checkPass = check(entry.getValue().getObjectForCheck());
|
||||
} catch (Exception e) {
|
||||
checkPass = false;
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
}
|
||||
|
||||
if ( ! checkPass) {
|
||||
if (!checkPass) {
|
||||
log.debug("一个链接检查未通过");
|
||||
// 检查未通过
|
||||
it.remove();
|
||||
|
||||
@@ -70,6 +90,7 @@ public class ThreadBasedConnectPool<T> {
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("连接池检测进程执行结束");
|
||||
}
|
||||
}, config.getMaxIdle(), config.getMaxIdle(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
@@ -114,7 +135,7 @@ public class ThreadBasedConnectPool<T> {
|
||||
map.remove(Thread.currentThread());
|
||||
}
|
||||
|
||||
protected void afterInit(ObjectInPool oip, T value ){
|
||||
protected void afterInit(ObjectInPool oip, T value) {
|
||||
|
||||
}
|
||||
|
||||
@@ -122,7 +143,7 @@ public class ThreadBasedConnectPool<T> {
|
||||
T value = initialValue();
|
||||
ObjectInPool opi = new ObjectInPool<>(value);
|
||||
map.put(Thread.currentThread(), opi);
|
||||
afterInit( opi , value);
|
||||
afterInit(opi, value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package sample;
|
||||
|
||||
import com.sunyard.SydApi;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
import racal.sunyard.main.SydApiBuilder;
|
||||
|
||||
public class ConnectThread extends Thread {
|
||||
|
||||
public SydApi getApi() {
|
||||
SydApiBuilder builder = new SydApiBuilder().setIp("172.16.18.59");
|
||||
SydApi4j papi = (SydApi4j) builder.build();
|
||||
// SydApi4j papi = (SydApi4j) new SydApi4j().connect("172.16.18.59" , 8889, null,1000);
|
||||
return papi;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
|
||||
// logger.info("测试开始");
|
||||
int n = Runtime.getRuntime().availableProcessors();
|
||||
int max = 1001;
|
||||
Thread[] tList = new Thread[max];
|
||||
for (int i = 0; i < max; i++) {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(Thread.currentThread().getName() + "已运行");
|
||||
ConnectThread stable = new ConnectThread();
|
||||
SydApi api = stable.getApi();
|
||||
try {
|
||||
// SydApi api = stable.getApi();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
tList[i] = thread;
|
||||
}
|
||||
for (Thread t : tList) {
|
||||
t.start();
|
||||
}
|
||||
for (Thread t : tList) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e3) {
|
||||
e3.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,13 @@ public class ShortLinkSample {
|
||||
// 路径要以 / 或 \\ 结尾表示目录
|
||||
|
||||
String[] pcipList = {"127.0.0.1", "192.168.0.200" };
|
||||
int[] iportList ={8080,8889};
|
||||
int[] iportList = {8080,8889};
|
||||
SydApi api = new SydApi4Database();
|
||||
try {
|
||||
api.SYD_Short_Connect_Ex(pcipList,iportList,10,10);
|
||||
|
||||
|
||||
|
||||
} finally {
|
||||
if ( null != api ) {
|
||||
api.SYD_Disconnect_Ex();
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
package test;
|
||||
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.database.Consts;
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.database.SydApiBuilder;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class BatchDataTest {
|
||||
|
||||
private SydApi api;
|
||||
private static class RandomTestData {
|
||||
private int size;
|
||||
private int[] keyIdx;
|
||||
private List<byte[]> data;
|
||||
|
||||
public int[] getKeyIdx() {
|
||||
return keyIdx;
|
||||
}
|
||||
|
||||
public List<byte[]> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public RandomTestData(int lucky) {
|
||||
this.size = lucky;
|
||||
this.keyIdx = new int[lucky];
|
||||
this.data = new ArrayList<>();
|
||||
for ( int i = 0 ; i < lucky; i++ ) {
|
||||
byte[] data = new byte[ new Random().nextInt(1024) ];
|
||||
new Random().nextBytes( data );
|
||||
this.data.add( data );
|
||||
}
|
||||
}
|
||||
public RandomTestData() {
|
||||
int lucky = new Random().nextInt(100);
|
||||
this.size = lucky;
|
||||
this.keyIdx = new int[lucky];
|
||||
this.data = new ArrayList<>();
|
||||
for ( int i = 0 ; i < lucky; i++ ) {
|
||||
byte[] data = new byte[ new Random().nextInt(1024) ];
|
||||
new Random().nextBytes( data );
|
||||
this.data.add( data );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
|
||||
SydApiBuilder builder = new SydApiBuilder(
|
||||
new String[]{"192.168.1.129"},
|
||||
new int[]{8889},
|
||||
10,
|
||||
15
|
||||
|
||||
);
|
||||
api = builder.build();
|
||||
}
|
||||
|
||||
@After
|
||||
public void end() {
|
||||
if (null != api) {
|
||||
api.SYD_Disconnect_Ex();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchSM4Case1() {
|
||||
// 密钥索引
|
||||
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
||||
// 测试数据
|
||||
List<byte[]> data = new ArrayList<>();
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
|
||||
List<byte[]> encData = api.SYD_SM4_BatchData(keyIdx, data, Consts.ECB_ENC);
|
||||
List<byte[]> orgData = api.SYD_SM4_BatchData(keyIdx, encData, Consts.ECB_DEC);
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
System.out.println("HEX=" + Util.bytes2HexString(encData.get(i)));
|
||||
Assert.assertArrayEquals(orgData.get(i), data.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchSM4Case2() {
|
||||
// 测试数据
|
||||
RandomTestData data = new RandomTestData();
|
||||
|
||||
List<byte[]> encData = api.SYD_SM4_BatchData(data.getKeyIdx(), data.getData(), Consts.ECB_ENC);
|
||||
List<byte[]> orgData = api.SYD_SM4_BatchData(data.getKeyIdx(), encData, Consts.ECB_DEC);
|
||||
|
||||
for (int i = 0; i < data.getSize(); i++) {
|
||||
System.out.println("HEX=" + Util.bytes2HexString(encData.get(i)));
|
||||
Assert.assertArrayEquals(orgData.get(i), data.getData().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchSM4CaseZero() {
|
||||
// 测试数据
|
||||
RandomTestData data = new RandomTestData(0);
|
||||
|
||||
List<byte[]> encData = api.SYD_SM4_BatchData(data.getKeyIdx(), data.getData(), Consts.ECB_ENC);
|
||||
List<byte[]> orgData = api.SYD_SM4_BatchData(data.getKeyIdx(), encData, Consts.ECB_DEC);
|
||||
|
||||
for (int i = 0; i < data.getSize(); i++) {
|
||||
System.out.println("HEX=" + Util.bytes2HexString(encData.get(i)));
|
||||
Assert.assertArrayEquals(orgData.get(i), data.getData().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchMACDataCase1() {
|
||||
// 密钥索引
|
||||
int[] keyIdx = new int[]{0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2}; // 12 组
|
||||
// 测试数据
|
||||
List<byte[]> data = new ArrayList<>();
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
data.add(new byte[8]);
|
||||
data.add(new byte[16]);
|
||||
data.add(new byte[1024]);
|
||||
|
||||
String[] macs = api.SYD_SM4Mac_BatchData(keyIdx, data);
|
||||
boolean[] checks = api.SYD_SM4Mac_BatchData(keyIdx, data, macs);
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
System.out.println("MAC[" + i + "]=" + macs[i]);
|
||||
Assert.assertTrue(checks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchMACData() {
|
||||
// 测试数据
|
||||
RandomTestData data = new RandomTestData();
|
||||
|
||||
String[] macs = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData());
|
||||
boolean[] checks = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData(), macs);
|
||||
|
||||
for (int i = 0; i < data.getSize(); i++) {
|
||||
System.out.println("MAC[" + i + "]=" + macs[i]);
|
||||
Assert.assertTrue(checks[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void batchMACDataZero() {
|
||||
// 测试数据
|
||||
RandomTestData data = new RandomTestData(0);
|
||||
|
||||
String[] macs = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData());
|
||||
boolean[] checks = api.SYD_SM4Mac_BatchData(data.getKeyIdx(), data.getData(), macs);
|
||||
|
||||
for (int i = 0; i < data.getSize(); i++) {
|
||||
System.out.println("MAC[" + i + "]=" + macs[i]);
|
||||
Assert.assertTrue(checks[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ public class FuncTest {
|
||||
|
||||
@Test
|
||||
public void syd_Short_Connect_Ex(){
|
||||
String[] pcipList = {"127.0.0.1", "192.168.0.100" };
|
||||
int[] iportList ={8080,8889};
|
||||
String[] pcipList = {"192.168.1.129", "192.168.1.129" };
|
||||
int[] iportList ={8889,8889};
|
||||
SydApi api = new SydApi4Database();
|
||||
try {
|
||||
api.SYD_Short_Connect_Ex(pcipList,iportList,10,10);
|
||||
@@ -49,8 +49,9 @@ public class FuncTest {
|
||||
|
||||
@Test
|
||||
public void originLong() throws Exception {
|
||||
SydApiBuilder builder = new SydApiBuilder(new String[]{"192.168.0.200"}, new int[]{8889}, 10, 15);
|
||||
SydApiBuilder builder = new SydApiBuilder(new String[]{"192.168.1.129"}, new int[]{8889}, 10, 15);
|
||||
SydApi.SYD_SetLogConfig("./logs/",3);
|
||||
|
||||
final SydApi api = builder.build();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int tmp = i;
|
||||
@@ -66,8 +67,9 @@ public class FuncTest {
|
||||
@Test
|
||||
public void originShort() throws Exception {
|
||||
SydApi.SYD_SetLogConfig("./logs/",3);
|
||||
String[] pcipList = {"192.168.0.100", "192.168.0.100", "192.168.0.100","192.168.0.100" };
|
||||
int[] iportList ={8080,8889,8889,8889};
|
||||
Logger log = LogFactory.getLogger();
|
||||
String[] pcipList = {"192.168.1.129", "192.168.1.129" };
|
||||
int[] iportList ={8080,8889};
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int tmp = i;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package test;
|
||||
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
import com.sunyard.sge.pool.PoolConfig;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class FutureTaskTest {
|
||||
|
||||
/**
|
||||
* 并发任务
|
||||
*/
|
||||
@Test
|
||||
public void case1() throws ExecutionException, InterruptedException, TimeoutException {
|
||||
PoolConfig poolConfig = new PoolConfig();
|
||||
CommWorker workers = new CommWorker(poolConfig);
|
||||
|
||||
long startAt = System.currentTimeMillis();
|
||||
try {
|
||||
FutureTask<Integer> task1 = workers.fTask(new Callable<Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
System.out.println( Thread.currentThread() + " task start");
|
||||
Thread.sleep( 1000 );
|
||||
System.out.println( Thread.currentThread() + " task over");
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
FutureTask<Integer> task2 = workers.fTask(new Callable<Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
System.out.println( Thread.currentThread() + " task start");
|
||||
Thread.sleep( 2000 );
|
||||
System.out.println( Thread.currentThread() + " task over");
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
FutureTask<Integer> task3 = workers.fTask(new Callable<Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
System.out.println( Thread.currentThread() + " task start");
|
||||
Thread.sleep( 3000 );
|
||||
System.out.println( Thread.currentThread() + " task over");
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
task1.get( );
|
||||
task2.get( );
|
||||
task3.get( );
|
||||
}finally {
|
||||
|
||||
long endAt = System.currentTimeMillis();
|
||||
|
||||
System.out.println("耗时 " + ( endAt - startAt ) );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,10 +19,10 @@ public class LongDataTest {
|
||||
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||
|
||||
SydApiBuilder builder = new SydApiBuilder(
|
||||
new String[]{"192.168.1.128"},
|
||||
new String[]{"192.168.1.129"},
|
||||
new int[]{8889},
|
||||
10,
|
||||
15
|
||||
30
|
||||
|
||||
);
|
||||
api = builder.build();
|
||||
@@ -30,7 +30,7 @@ public class LongDataTest {
|
||||
|
||||
@Test
|
||||
public void sm3_case1() {
|
||||
SydApiSM3 sm3 = api.initSM3();
|
||||
ISM3 sm3 = api.initSM3();
|
||||
sm3.update(null);
|
||||
String mac = sm3.digest();
|
||||
Assert.assertEquals("1AB21D8355CFA17F8E61194831E81A8F22BEC8C728FEFB747ED035EB5082AA2B", mac);
|
||||
@@ -38,7 +38,7 @@ public class LongDataTest {
|
||||
|
||||
@Test
|
||||
public void sm3_case2() {
|
||||
SydApiSM3 sm3 = api.initSM3();
|
||||
ISM3 sm3 = api.initSM3();
|
||||
sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"));
|
||||
String mac = sm3.digest();
|
||||
Assert.assertEquals("9431C865C84B02C8A7C503F53A6D6C3C66704F8F713D124610314E31B19937F7", mac);
|
||||
@@ -46,7 +46,7 @@ public class LongDataTest {
|
||||
|
||||
@Test
|
||||
public void sm3_case3() {
|
||||
SydApiSM3 sm3 = api.initSM3();
|
||||
ISM3 sm3 = api.initSM3();
|
||||
sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"));
|
||||
sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"));
|
||||
String mac = sm3.digest();
|
||||
@@ -55,7 +55,7 @@ public class LongDataTest {
|
||||
|
||||
@Test
|
||||
public void sm3_case4() {
|
||||
SydApiSM3 sm3 = api.initSM3();
|
||||
ISM3 sm3 = api.initSM3();
|
||||
System.out.print("org=");
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int lucky = new Random().nextInt(4065);
|
||||
@@ -73,49 +73,44 @@ public class LongDataTest {
|
||||
|
||||
@Test
|
||||
public void sm3_case5() {
|
||||
SydApiSM3 sm3 = api.initSM3();
|
||||
ISM3 sm3 = api.initSM3();
|
||||
String mac = sm3.digest();
|
||||
Assert.assertEquals("1AB21D8355CFA17F8E61194831E81A8F22BEC8C728FEFB747ED035EB5082AA2B", mac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4mac_case1() {
|
||||
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
||||
ISM4Mac sydApiMac = api.initSM4Mac(1);
|
||||
sydApiMac.update(null);
|
||||
String mac = (String) sydApiMac.disget().getData();
|
||||
String mac = sydApiMac.disget();
|
||||
Assert.assertEquals("FF49F76AD3A71651FE1A8A9F0D2A689E", mac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4mac_case2() {
|
||||
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
||||
String mac = (String) sydApiMac.disget().getData();
|
||||
ISM4Mac sydApiMac = api.initSM4Mac(1);
|
||||
String mac = sydApiMac.disget();
|
||||
Assert.assertEquals("FF49F76AD3A71651FE1A8A9F0D2A689E", mac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sm4mac_case3() {
|
||||
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
||||
ISM4Mac sydApiMac = api.initSM4Mac(1);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// int lucky = new Random().nextInt(4065);
|
||||
byte[] data = new byte[66];
|
||||
// System.out.println("len=" + lucky);
|
||||
// new Random().nextBytes( data );
|
||||
// sydApiMac.setPcMac("405C2DCF4884240B7F2E4FF56B00639A");
|
||||
sydApiMac.setPcMac("72A7EFF0004CE7D2A1B7C2260726EC38");
|
||||
sydApiMac.update(data);
|
||||
}
|
||||
|
||||
boolean mac = (boolean) sydApiMac.disget().getData();
|
||||
// String mac1 = (String) sydApiMac.disget().getData();
|
||||
String mac = (String) sydApiMac.disget();
|
||||
System.out.println(mac);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void case5() {
|
||||
SydApiSM4 sydApiSM4 = api.initSM4(1, 1);
|
||||
ISM4 sydApiSM4 = api.initSM4(1, 1);
|
||||
byte[] enData = sydApiSM4.finish();
|
||||
System.out.println(Util.bytes2HexString(enData));
|
||||
Assert.assertArrayEquals(Util.hexString2Bytes("FF49F76AD3A71651FE1A8A9F0D2A689E"), enData);
|
||||
@@ -123,7 +118,7 @@ public class LongDataTest {
|
||||
|
||||
@Test
|
||||
public void case6() {
|
||||
SydApiSM4 sydApiSM4 = api.initSM4(1, 1);
|
||||
ISM4 sydApiSM4 = api.initSM4(1, 1);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int lucky = new Random().nextInt(4065);
|
||||
@@ -144,8 +139,8 @@ public class LongDataTest {
|
||||
|
||||
int max = 1000;
|
||||
int size = 4065;
|
||||
SydApiSM4 en = api.initSM4(1, 1);
|
||||
SydApiSM4 de = api.initSM4(1, 0);
|
||||
ISM4 en = api.initSM4(1, 1);
|
||||
ISM4 de = api.initSM4(1, 0);
|
||||
|
||||
ByteBuffer inData = ByteBuffer.allocate(max * size);
|
||||
ByteBuffer enData = ByteBuffer.allocate(max * size + 16);
|
||||
|
||||
@@ -92,13 +92,12 @@ package test;
|
||||
//}
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
if (16 > 1799 + 16
|
||||
||
|
||||
(1799 + 16)/16<2
|
||||
) {
|
||||
System.out.println("1");
|
||||
}
|
||||
List<Integer> keyGroups = new ArrayList<>();
|
||||
keyGroups.toArray( new Integer[0] );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user