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);
|
||||
}
|
||||
|
||||
// 保活接口
|
||||
|
||||
Reference in New Issue
Block a user