Merge remote-tracking branch 'origin/dev-1.0' into dev-1.0
This commit is contained in:
commit
6a5a335b24
@ -43,8 +43,7 @@ public interface SydApi {
|
|||||||
* @param iDealTimeOut 交易接口超时设定,单位毫秒(不超过10ms)。
|
* @param iDealTimeOut 交易接口超时设定,单位毫秒(不超过10ms)。
|
||||||
* @return SydApi 的对象,只要有设备可用则返回。
|
* @return SydApi 的对象,只要有设备可用则返回。
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
SydApi SYD_Short_Connect_Ex(
|
||||||
SydApi SYD_Connect_Ex(
|
|
||||||
String[] pcIpList,
|
String[] pcIpList,
|
||||||
int[] iPortList,
|
int[] iPortList,
|
||||||
int iConnectTimeOut,
|
int iConnectTimeOut,
|
||||||
|
|||||||
@ -14,11 +14,14 @@ import java.util.List;
|
|||||||
* 上海黄金交易所 - 数据库加密方案
|
* 上海黄金交易所 - 数据库加密方案
|
||||||
*/
|
*/
|
||||||
public class SydApi4Database implements SydApi {
|
public class SydApi4Database implements SydApi {
|
||||||
Logger log = LogFactory.getLogger();
|
|
||||||
|
static {
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||||
|
}
|
||||||
|
|
||||||
private SydApi4j[] hsms = null;
|
private SydApi4j[] hsms = null;
|
||||||
private boolean isShortLinkMode = false;
|
private boolean isShortLinkMode = false;
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public SydApi4Database() {
|
public SydApi4Database() {
|
||||||
isShortLinkMode = true;
|
isShortLinkMode = true;
|
||||||
}
|
}
|
||||||
@ -28,37 +31,69 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SydApi SYD_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iDealTimeOut) {
|
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, iDealTimeOut);
|
if (LogFactory.iLogLevel == 3) {
|
||||||
|
log.debug("SYD_Connect_Ex方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList), Arrays.toString(iPortList), iConnectTimeOut, allDealTimeOut);
|
||||||
}
|
}
|
||||||
SydApi4j api = (SydApi4j) new SydApi4j().connect(pcIpList[0], iPortList[0], null, iConnectTimeOut);
|
|
||||||
hsms = new SydApi4j[]{api};
|
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) {
|
||||||
|
// TODO
|
||||||
|
throw new SydApiException("连接错误", -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
hsms = apis;
|
||||||
|
isShortLinkMode = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void SYD_Disconnect_Ex() {
|
public void SYD_Disconnect_Ex() {
|
||||||
if (isShortLinkMode) {
|
if (isShortLinkMode) {
|
||||||
if ( null == hsms ){
|
if (null == hsms) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SydApi4j api = hsms[0];
|
for (int i = 0; i < hsms.length; i++) {
|
||||||
if (null != api) {
|
SydApi4j api = hsms[i];
|
||||||
try {
|
if (null != api) {
|
||||||
api.disconnect();
|
try {
|
||||||
} catch (Exception e) {
|
api.disconnect();
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
api = null;
|
|
||||||
}
|
}
|
||||||
// 配合链接池,不再需要释放。
|
// 配合链接池,不再需要释放。
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM3_Hash_ShortData方法接收参数:pcData:{}", Arrays.toString(pcData));
|
log.debug("SYD_SM3_Hash_ShortData方法接收参数:pcData:{}", Arrays.toString(pcData));
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -67,13 +102,19 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||||
log.debug("api.SYD_SM3_LongData方法返回值:{}",Arrays.toString(hash));
|
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||||
return Util.bytes2HexString(hash);
|
return Util.bytes2HexString(hash);
|
||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -84,7 +125,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM3_Hash_LongData方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData);
|
log.debug("SYD_SM3_Hash_LongData方法接收参数:pcData:{},iPkgNum:{},pcProcData:{}", Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,14 +137,17 @@ public class SydApi4Database implements SydApi {
|
|||||||
byte[] procData = null;
|
byte[] procData = null;
|
||||||
if (2 == iPkgNum || 3 == iPkgNum) {
|
if (2 == iPkgNum || 3 == iPkgNum) {
|
||||||
procData = Util.hexString2Bytes(pcProcData);
|
procData = Util.hexString2Bytes(pcProcData);
|
||||||
log.debug("Util.hexString2Bytes方法返回值:{}",Arrays.toString(procData));
|
log.debug("Util.hexString2Bytes方法返回值:{}", Arrays.toString(procData));
|
||||||
|
|
||||||
}
|
}
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
byte[] hash = api.SYD_SM3_LongData(pcData, procData, iPkgNum);
|
||||||
log.debug("api.SYD_SM3_LongData方法返回值:{}",Arrays.toString(hash));
|
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||||
if (0 == iPkgNum || 3 == iPkgNum) {
|
if (0 == iPkgNum || 3 == iPkgNum) {
|
||||||
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
return new LongDataReturn<String, String>(null, Util.bytes2HexString(hash));
|
||||||
} else {
|
} else {
|
||||||
@ -111,6 +156,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -121,10 +167,11 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iFlag);
|
log.debug("SYD_SM4_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iFlag);
|
||||||
}
|
}
|
||||||
if (iFlag==Consts.ECB_DEC&&null==pcData||pcData.length==0){
|
if (iFlag == Consts.ECB_DEC && null == pcData || pcData.length == 0) {
|
||||||
throw new SydApiException(19);
|
throw new SydApiException(19);
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -134,8 +181,10 @@ public class SydApi4Database implements SydApi {
|
|||||||
String keyIndex = String.format("%08d", iKeyIndex);
|
String keyIndex = String.format("%08d", iKeyIndex);
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (Consts.ECB_ENC == iFlag) {
|
if (Consts.ECB_ENC == iFlag) {
|
||||||
return api.SYMEncryptData(
|
return api.SYMEncryptData(
|
||||||
com.sunyard.SydApi.NALG_SM4,
|
com.sunyard.SydApi.NALG_SM4,
|
||||||
@ -152,6 +201,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -162,12 +212,13 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},iFlag:{}", iKeyIndex, pcData.size(), iFlag);
|
log.debug("SYD_SM4_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},iFlag:{}", iKeyIndex, pcData.size(), iFlag);
|
||||||
}
|
}
|
||||||
if (iFlag==Consts.ECB_DEC){
|
if (iFlag == Consts.ECB_DEC) {
|
||||||
for (byte[] pcdata:pcData){
|
for (byte[] pcdata : pcData) {
|
||||||
if (null==pcData||pcdata.length==0) {
|
if (null == pcData || pcdata.length == 0) {
|
||||||
throw new SydApiException(19);
|
throw new SydApiException(19);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -183,6 +234,9 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (Consts.ECB_ENC == iFlag) {
|
if (Consts.ECB_ENC == iFlag) {
|
||||||
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
return api.SYMEncryptDecryptBatchData(8, 1, null, keys, 1, pcData);
|
||||||
@ -193,6 +247,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -203,10 +258,11 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
public byte[] SYD_SM4_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag);
|
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){
|
if (iFlag == Consts.ECB_DEC && null == pcData || pcData.length == 0) {
|
||||||
throw new SydApiException(19);
|
throw new SydApiException(19);
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -216,6 +272,9 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
if (Consts.ECB_ENC == iFlag) {
|
if (Consts.ECB_ENC == iFlag) {
|
||||||
return api.SYD_SM4_LongData(
|
return api.SYD_SM4_LongData(
|
||||||
@ -236,6 +295,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -246,7 +306,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4Mac_BatchData:iKeyIndex:{},pcData元素个数为:{}", iKeyIndex, pcData.size());
|
log.debug("SYD_SM4Mac_BatchData:iKeyIndex:{},pcData元素个数为:{}", iKeyIndex, pcData.size());
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -259,6 +320,9 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||||
String[] ret = new String[list.size()];
|
String[] ret = new String[list.size()];
|
||||||
@ -273,6 +337,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -283,7 +348,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4Mac_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), Arrays.toString(pcMac));
|
log.debug("SYD_SM4Mac_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},pcMac:{}", iKeyIndex, pcData.size(), Arrays.toString(pcMac));
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -296,6 +362,9 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
List<byte[]> list = api.SYMEncryptDecryptBatchData(8, 2, null, keys, 1, pcData);
|
||||||
boolean[] ret = new boolean[list.size()];
|
boolean[] ret = new boolean[list.size()];
|
||||||
@ -310,6 +379,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -320,7 +390,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData);
|
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData);
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -338,6 +409,9 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
byte[] ret = api.SYD_SM4_LongData(
|
byte[] ret = api.SYD_SM4_LongData(
|
||||||
8,
|
8,
|
||||||
@ -360,6 +434,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -370,7 +445,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LongDataReturn<String, Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) {
|
public LongDataReturn<String, Boolean> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData, String pcMac) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData, pcMac);
|
log.debug("SYD_SM4Mac_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},pcProcData:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, pcProcData, pcMac);
|
||||||
}
|
}
|
||||||
LongDataReturn<String, String> ret = SYD_SM4Mac_LongData(iKeyIndex, pcData, iPkgNum, pcProcData);
|
LongDataReturn<String, String> ret = SYD_SM4Mac_LongData(iKeyIndex, pcData, iPkgNum, pcProcData);
|
||||||
@ -386,7 +462,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
public String SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData));
|
log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData));
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -394,9 +471,12 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
|
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
|
||||||
log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}",keyIndex);
|
log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}", keyIndex);
|
||||||
SydApiException le = null;
|
SydApiException le = null;
|
||||||
for (SydApi4j api : this.hsms) {
|
for (SydApi4j api : this.hsms) {
|
||||||
|
if (null == api) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
|
|
||||||
return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
|
return api.SYD_SM4Mac_ShortData(keyIndex, pcData);
|
||||||
@ -404,6 +484,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
} catch (SydApiException e) {
|
} catch (SydApiException e) {
|
||||||
le = e;
|
le = e;
|
||||||
if (e.getRetCode() < 0) {
|
if (e.getRetCode() < 0) {
|
||||||
|
log.error("HA 重试");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
@ -414,7 +495,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
public boolean SYD_SM4Mac_ShortData(int iKeyIndex, byte[] pcData, String pcMac) {
|
||||||
if (LogFactory.iLogLevel==3) {
|
Logger log = LogFactory.getLogger();
|
||||||
|
if (LogFactory.iLogLevel == 3) {
|
||||||
log.debug("SYD_SM4Mac_ShortData方法接收参数:{}", iKeyIndex, Arrays.toString(pcData), pcMac);
|
log.debug("SYD_SM4Mac_ShortData方法接收参数:{}", iKeyIndex, Arrays.toString(pcData), pcMac);
|
||||||
}
|
}
|
||||||
if (null == this.hsms || this.hsms.length < 1) {
|
if (null == this.hsms || this.hsms.length < 1) {
|
||||||
@ -426,7 +508,7 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData);
|
String mac = this.SYD_SM4Mac_ShortData(iKeyIndex, pcData);
|
||||||
log.debug(this+".SYD_SM4Mac_ShortData方法返回值:{}",mac);
|
log.debug(this + ".SYD_SM4Mac_ShortData方法返回值:{}", mac);
|
||||||
return pcMac.equals(mac);
|
return pcMac.equals(mac);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,9 @@ import org.apache.logging.log4j.Logger;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class SydApiBuilder {
|
public class SydApiBuilder {
|
||||||
Logger log = LogFactory.getLogger();
|
static {
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||||
|
}
|
||||||
private SydApiPool pool;
|
private SydApiPool pool;
|
||||||
public SydApiBuilder(
|
public SydApiBuilder(
|
||||||
String[] pcIpList,
|
String[] pcIpList,
|
||||||
@ -16,6 +18,7 @@ public class SydApiBuilder {
|
|||||||
int iConnectTimeOut,
|
int iConnectTimeOut,
|
||||||
int iDealTimeOut
|
int iDealTimeOut
|
||||||
) {
|
) {
|
||||||
|
Logger log = LogFactory.getLogger();
|
||||||
log.debug("创建链接");
|
log.debug("创建链接");
|
||||||
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
|
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
|
||||||
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
||||||
@ -36,7 +39,7 @@ public class SydApiBuilder {
|
|||||||
SydApi api = null;
|
SydApi api = null;
|
||||||
try {
|
try {
|
||||||
api = new SydApi4Database();
|
api = new SydApi4Database();
|
||||||
api.SYD_Connect_Ex( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
|
api.SYD_Short_Connect_Ex( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@ -44,6 +47,7 @@ public class SydApiBuilder {
|
|||||||
|
|
||||||
}catch ( Exception e) {
|
}catch ( Exception e) {
|
||||||
// 忽略
|
// 忽略
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.sunyard.sge.database.pool;
|
package com.sunyard.sge.database.pool;
|
||||||
|
|
||||||
|
|
||||||
|
import com.sunyard.sge.database.SydApi;
|
||||||
import com.sunyard.sge.log.LogFactory;
|
import com.sunyard.sge.log.LogFactory;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@ -11,6 +12,10 @@ import java.util.Arrays;
|
|||||||
* 到加密机的链接信息
|
* 到加密机的链接信息
|
||||||
*/
|
*/
|
||||||
public class HsmLinkInfo {
|
public class HsmLinkInfo {
|
||||||
|
static {
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||||
|
}
|
||||||
|
static Logger log = LogFactory.getLogger();
|
||||||
private String pcIp;
|
private String pcIp;
|
||||||
private int iPort;
|
private int iPort;
|
||||||
private int iConnectTimeOut;
|
private int iConnectTimeOut;
|
||||||
@ -25,7 +30,7 @@ public class HsmLinkInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static HsmLinkInfo[] mutiHsmLinkInfo(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iAllDealTimeOut) {
|
public static HsmLinkInfo[] mutiHsmLinkInfo(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iAllDealTimeOut) {
|
||||||
Logger log = LogFactory.getLogger();
|
|
||||||
log.debug("mutiHsmLinkInfo方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iAllDealTimeOut);
|
log.debug("mutiHsmLinkInfo方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iAllDealTimeOut);
|
||||||
log.debug("链接参数整理");
|
log.debug("链接参数整理");
|
||||||
if (null == pcIpList &&
|
if (null == pcIpList &&
|
||||||
|
|||||||
@ -1,11 +1,17 @@
|
|||||||
package com.sunyard.sge.database.pool;
|
package com.sunyard.sge.database.pool;
|
||||||
|
|
||||||
|
import com.sunyard.SydApiException;
|
||||||
import com.sunyard.sge.database.SydApi;
|
import com.sunyard.sge.database.SydApi;
|
||||||
import com.sunyard.sge.database.SydApi4Database;
|
import com.sunyard.sge.database.SydApi4Database;
|
||||||
|
import com.sunyard.sge.log.LogFactory;
|
||||||
import com.sunyard.sge.pool.ThreadBasedConnectPool;
|
import com.sunyard.sge.pool.ThreadBasedConnectPool;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||||
|
static {
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||||
|
}
|
||||||
|
|
||||||
private HsmLinkInfo[] linkInfos;
|
private HsmLinkInfo[] linkInfos;
|
||||||
|
|
||||||
@ -33,10 +39,18 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
|||||||
);
|
);
|
||||||
connected = true;
|
connected = true;
|
||||||
} catch ( Exception e ) {
|
} catch ( Exception e ) {
|
||||||
|
Logger log = LogFactory.getLogger();
|
||||||
// 忽略单个错误
|
// 忽略单个错误
|
||||||
|
log.error("网络连接错误 {} {} - 忽略",info.getPcIp(),info.getiPort());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( ! connected ) {
|
||||||
|
// TODO
|
||||||
|
throw new SydApiException("连接错误",-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return new SydApi4Database( apis );
|
return new SydApi4Database( apis );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -62,7 +62,7 @@ public class LogFactory {
|
|||||||
|
|
||||||
// 日志打印方式——输出为文件
|
// 日志打印方式——输出为文件
|
||||||
Appender appender = FileAppender.createAppender(
|
Appender appender = FileAppender.createAppender(
|
||||||
String.format( pcLogPath + getToday() + ".log"),
|
String.format( pcLogPath +"sydapi_"+getToday() + ".log"),
|
||||||
"true", "false",
|
"true", "false",
|
||||||
logName, null, "true", "true", null, layout, null, null, null, config);
|
logName, null, "true", "true", null, layout, null, null, null, config);
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package com.sunyard.sge.pool;
|
package com.sunyard.sge.pool;
|
||||||
|
|
||||||
|
import com.sunyard.sge.database.SydApi;
|
||||||
|
import com.sunyard.sge.database.SydApi4Database;
|
||||||
import com.sunyard.sge.log.LogFactory;
|
import com.sunyard.sge.log.LogFactory;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
@ -12,7 +14,9 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class ThreadBasedConnectPool<T> {
|
public class ThreadBasedConnectPool<T> {
|
||||||
|
static {
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||||
|
}
|
||||||
|
|
||||||
// ThreadLocal
|
// ThreadLocal
|
||||||
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
|
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
|
||||||
|
|||||||
@ -16,7 +16,7 @@ public class Sample
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// 路径要以 / 或 \\ 结尾表示目录
|
// 路径要以 / 或 \\ 结尾表示目录
|
||||||
SydApi.SYD_SetLogConfig("./logs/",1);
|
SydApi.SYD_SetLogConfig("./logs/",3);
|
||||||
|
|
||||||
Logger log = LogFactory.getLogger();
|
Logger log = LogFactory.getLogger();
|
||||||
log.info("start");
|
log.info("start");
|
||||||
@ -373,13 +373,9 @@ public class Sample
|
|||||||
int keyIdx = 0;
|
int keyIdx = 0;
|
||||||
// 测试数据
|
// 测试数据
|
||||||
byte[] data = new byte[16];
|
byte[] data = new byte[16];
|
||||||
byte[] data2 = {};
|
|
||||||
byte[] encData = api.SYD_SM4_ShortData( keyIdx , data, Consts.ECB_ENC );
|
byte[] encData = api.SYD_SM4_ShortData( keyIdx , data, Consts.ECB_ENC );
|
||||||
byte[] orgData = api.SYD_SM4_ShortData( keyIdx , encData, Consts.ECB_DEC );
|
byte[] orgData = api.SYD_SM4_ShortData( keyIdx , encData, Consts.ECB_DEC );
|
||||||
//测试解密 pcData长度为0的情况
|
|
||||||
byte[] pcData_0 = api.SYD_SM4_ShortData( keyIdx , data2, Consts.ECB_DEC );
|
|
||||||
System.out.println( "HEX=" + Util.bytes2HexString( encData ) );
|
System.out.println( "HEX=" + Util.bytes2HexString( encData ) );
|
||||||
System.out.println( "HEX=" + Util.bytes2HexString( pcData_0 ) );
|
|
||||||
Assert.assertArrayEquals( orgData, data );
|
Assert.assertArrayEquals( orgData, data );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,24 +387,14 @@ public class Sample
|
|||||||
data.add( new byte[8] );
|
data.add( new byte[8] );
|
||||||
data.add( new byte[16] );
|
data.add( new byte[16] );
|
||||||
data.add( new byte[1024] );
|
data.add( new byte[1024] );
|
||||||
List<byte[]> data2 = new ArrayList<>();
|
|
||||||
data2.add( new byte[0] );
|
|
||||||
data2.add( new byte[0] );
|
|
||||||
data2.add( new byte[0] );
|
|
||||||
|
|
||||||
List<byte[]> encData = api.SYD_SM4_BatchData( keyIdx , data, Consts.ECB_ENC );
|
List<byte[]> encData = api.SYD_SM4_BatchData( keyIdx , data, Consts.ECB_ENC );
|
||||||
List<byte[]> orgData = api.SYD_SM4_BatchData( keyIdx , encData, Consts.ECB_DEC );
|
List<byte[]> orgData = api.SYD_SM4_BatchData( keyIdx , encData, Consts.ECB_DEC );
|
||||||
//测试解密 pcData长度为0的情况
|
|
||||||
List<byte[]> pcData_0 = api.SYD_SM4_BatchData( keyIdx ,data2 , Consts.ECB_DEC );
|
|
||||||
|
|
||||||
|
|
||||||
for ( int i = 0 ; i < data.size() ; i++ ) {
|
for ( int i = 0 ; i < data.size() ; i++ ) {
|
||||||
System.out.println( "HEX=" + Util.bytes2HexString( encData.get( i ) ) );
|
System.out.println( "HEX=" + Util.bytes2HexString( encData.get( i ) ) );
|
||||||
Assert.assertArrayEquals( orgData.get( i ), data.get( i ) );
|
Assert.assertArrayEquals( orgData.get( i ), data.get( i ) );
|
||||||
}
|
}
|
||||||
for ( int i = 0 ; i < data2.size() ; i++ ) {
|
|
||||||
System.out.println( "HEX=" + Util.bytes2HexString( encData.get( i ) ) );
|
|
||||||
Assert.assertArrayEquals( pcData_0.get( i ), data.get( i ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
25
src/test/java/sample/ShortLinkSample.java
Normal file
25
src/test/java/sample/ShortLinkSample.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package sample;
|
||||||
|
|
||||||
|
import com.sunyard.sge.database.SydApi;
|
||||||
|
import com.sunyard.sge.database.SydApi4Database;
|
||||||
|
|
||||||
|
public class ShortLinkSample {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// 路径要以 / 或 \\ 结尾表示目录
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/",3);
|
||||||
|
|
||||||
|
String[] pcipList = {"127.0.0.1", "192.168.0.200" };
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,9 +1,13 @@
|
|||||||
package test;
|
package test;
|
||||||
|
|
||||||
|
import com.sunyard.sge.database.Consts;
|
||||||
import com.sunyard.sge.database.SydApi;
|
import com.sunyard.sge.database.SydApi;
|
||||||
|
import com.sunyard.sge.database.SydApi4Database;
|
||||||
import com.sunyard.sge.database.SydApiBuilder;
|
import com.sunyard.sge.database.SydApiBuilder;
|
||||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||||
import com.sunyard.sge.database.pool.SydApiPool;
|
import com.sunyard.sge.database.pool.SydApiPool;
|
||||||
|
import com.sunyard.sge.log.LogFactory;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -23,6 +27,21 @@ public class FuncTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void syd_Short_Connect_Ex(){
|
||||||
|
String[] pcipList = {"127.0.0.1", "192.168.0.200" };
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sm3(){
|
public void sm3(){
|
||||||
SydApiBuilder builder = new SydApiBuilder(
|
SydApiBuilder builder = new SydApiBuilder(
|
||||||
@ -35,4 +54,49 @@ public class FuncTest {
|
|||||||
api = builder.build();
|
api = builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void originLong() throws Exception {
|
||||||
|
SydApiBuilder builder = new SydApiBuilder(new String[]{"192.168.0.200"}, new int[]{8889}, 10, 15);
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/",3);
|
||||||
|
Logger log = LogFactory.getLogger();
|
||||||
|
final SydApi api = builder.build();
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
int tmp = i;
|
||||||
|
new Thread(() -> {
|
||||||
|
for (int j = 0; j < 20; j++) {
|
||||||
|
byte[] bytes = api.SYD_SM4_ShortData(0, (tmp + "t" + j).getBytes(), Consts.ECB_ENC);
|
||||||
|
byte[] result = api.SYD_SM4_ShortData(0, bytes, Consts.ECB_DEC);
|
||||||
|
System.out.println(Thread.currentThread().getName() + "," + new String(result));
|
||||||
|
}
|
||||||
|
}, i + "t").start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void originShort() throws Exception {
|
||||||
|
SydApi.SYD_SetLogConfig("./logs/",3);
|
||||||
|
Logger log = LogFactory.getLogger();
|
||||||
|
String[] pcipList = {"192.168.0.200", "192.168.0.200", "192.168.0.200" };
|
||||||
|
int[] iportList ={8080,8889,8889};
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
int tmp = i;
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
SydApi api = new SydApi4Database();
|
||||||
|
for (int j = 0; j < 20; j++) {
|
||||||
|
api.SYD_Short_Connect_Ex(pcipList,iportList,10,15);
|
||||||
|
byte[] bytes = api.SYD_SM4_ShortData(0, (tmp + "t" + j).getBytes(), Consts.ECB_ENC);
|
||||||
|
byte[] result = api.SYD_SM4_ShortData(0, bytes, Consts.ECB_DEC);
|
||||||
|
System.out.println(Thread.currentThread().getName() + "," + new String(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if ( null != api ) {
|
||||||
|
api.SYD_Disconnect_Ex();
|
||||||
|
}
|
||||||
|
} } , i + "t").start();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
169
src/test/java/test/LengthTest.java
Normal file
169
src/test/java/test/LengthTest.java
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import com.sunyard.proto.Util;
|
||||||
|
import com.sunyard.sge.database.*;
|
||||||
|
import com.sunyard.sge.database.Consts;
|
||||||
|
import com.sunyard.sge.log.LogFactory;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class LengthTest
|
||||||
|
{
|
||||||
|
private SydApi api;
|
||||||
|
@Before
|
||||||
|
public void init(){
|
||||||
|
SydApiBuilder builder = new SydApiBuilder(
|
||||||
|
new String[]{ "192.168.0.200" },
|
||||||
|
new int[]{ 8889 },
|
||||||
|
10,
|
||||||
|
15
|
||||||
|
|
||||||
|
);
|
||||||
|
api = builder.build();
|
||||||
|
}
|
||||||
|
//
|
||||||
|
@Test
|
||||||
|
public void syd_SM4_ShortDataTest(){
|
||||||
|
int keyIdx = 0;
|
||||||
|
byte[] data = new byte[16];
|
||||||
|
//解密 pcData长度为0
|
||||||
|
byte[] bytes = api.SYD_SM4_ShortData(0, new byte[0], Consts.ECB_DEC);
|
||||||
|
Arrays.toString(bytes);
|
||||||
|
//解密 pcData长度不为0
|
||||||
|
byte[] encData = api.SYD_SM4_ShortData( keyIdx , data, Consts.ECB_ENC );
|
||||||
|
byte[] bytes2 = api.SYD_SM4_ShortData(keyIdx, encData, Consts.ECB_DEC);
|
||||||
|
Arrays.toString(bytes2);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void syd_SM4_BatchDataTest(){
|
||||||
|
int[] keyIdx = new int[]{ 0, 1, 2 };
|
||||||
|
// 测试数据
|
||||||
|
List<byte[]> data2 = new ArrayList<>();
|
||||||
|
data2.add( new byte[8] );
|
||||||
|
data2.add( new byte[16] );
|
||||||
|
data2.add( new byte[1024] );
|
||||||
|
List<byte[]> data = new ArrayList<>();
|
||||||
|
data.add( new byte[0] );
|
||||||
|
data.add( new byte[0] );
|
||||||
|
data.add( new byte[0] );
|
||||||
|
|
||||||
|
// 解密 pcData长度为0
|
||||||
|
List<byte[]> orgDatas = api.SYD_SM4_BatchData( keyIdx , data, Consts.ECB_DEC );
|
||||||
|
for (byte[] byte_1 :orgDatas) {
|
||||||
|
System.out.printf("解密后数据", Arrays.toString(byte_1));
|
||||||
|
}
|
||||||
|
//解密 pcData长度不为0
|
||||||
|
List<byte[]> encData = api.SYD_SM4_BatchData( keyIdx , data2, Consts.ECB_ENC );
|
||||||
|
List<byte[]> orgDatas2 = api.SYD_SM4_BatchData( keyIdx , encData, Consts.ECB_DEC );
|
||||||
|
for (byte[] byte_2 :orgDatas2) {
|
||||||
|
System.out.println(Arrays.toString(byte_2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void syd_SM4_LongDataTest(){
|
||||||
|
// 密钥索引
|
||||||
|
int keyIdx = 0;
|
||||||
|
// 测试数据
|
||||||
|
byte[] data = new byte[16 * 3 + 8];
|
||||||
|
// 每包大小
|
||||||
|
int pack = 16;
|
||||||
|
//解密 pcData长度为0
|
||||||
|
byte[] recv = api.SYD_SM4_LongData( keyIdx , new byte[0], Consts.PkgNumHead, Consts.ECB_DEC);
|
||||||
|
System.out.println(Arrays.toString(recv));
|
||||||
|
//解密 pcData长度不为0
|
||||||
|
// 密文数据
|
||||||
|
ByteBuffer enDataBuff = ByteBuffer.allocate( data.length + 16 );
|
||||||
|
// 分包加密
|
||||||
|
{
|
||||||
|
int n = data.length / pack;
|
||||||
|
int remainder = data.length % pack;
|
||||||
|
if ( remainder > 0 ) {
|
||||||
|
n += 1;
|
||||||
|
} else {
|
||||||
|
remainder = pack;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( int i = 0 ; i < n ; i ++ ) {
|
||||||
|
byte[] buff = new byte[ pack ];
|
||||||
|
|
||||||
|
// 首包
|
||||||
|
if ( 0 == i ) {
|
||||||
|
System.arraycopy( data, i * pack, buff, 0, pack );
|
||||||
|
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumHead, Consts.ECB_ENC);
|
||||||
|
enDataBuff.put( recv3 );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 尾包
|
||||||
|
if ( i == (n-1) ){
|
||||||
|
buff = new byte[ remainder ];
|
||||||
|
System.arraycopy( data, i * pack, buff, 0, remainder );
|
||||||
|
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumTail, Consts.ECB_ENC);
|
||||||
|
enDataBuff.put( recv3);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 中间包
|
||||||
|
System.arraycopy( data, i * pack, buff, 0, pack );
|
||||||
|
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumMiddle, Consts.ECB_ENC);
|
||||||
|
enDataBuff.put( recv3 );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
byte[] enData = new byte[ enDataBuff.position() ];
|
||||||
|
System.arraycopy( enDataBuff.array(), 0, enData, 0, enData.length );
|
||||||
|
System.out.println( "EnData HEX=" + Util.bytes2HexString( enData ));
|
||||||
|
|
||||||
|
|
||||||
|
// 分包解密
|
||||||
|
// 密文数据
|
||||||
|
ByteBuffer deDataBuff = ByteBuffer.allocate( data.length + 16 );
|
||||||
|
{
|
||||||
|
int n = enData.length / pack;
|
||||||
|
int remainder = enData.length % pack;
|
||||||
|
if ( remainder > 0 ) {
|
||||||
|
n += 1;
|
||||||
|
} else {
|
||||||
|
remainder = pack;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( int i = 0 ; i < n ; i ++ ) {
|
||||||
|
byte[] buff = new byte[ pack ];
|
||||||
|
|
||||||
|
// 首包
|
||||||
|
if ( 0 == i ) {
|
||||||
|
System.arraycopy( enData, i * pack, buff, 0, pack );
|
||||||
|
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumHead, Consts.ECB_DEC);
|
||||||
|
deDataBuff.put( recv3 );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尾包
|
||||||
|
if ( i == (n-1) ){
|
||||||
|
buff = new byte[ remainder ];
|
||||||
|
System.arraycopy( enData, i * pack, buff, 0, remainder );
|
||||||
|
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumTail, Consts.ECB_DEC);
|
||||||
|
deDataBuff.put( recv3 );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 中间包
|
||||||
|
System.arraycopy( enData, i * pack, buff, 0, pack );
|
||||||
|
byte[] recv3 = api.SYD_SM4_LongData( keyIdx , buff, Consts.PkgNumMiddle, Consts.ECB_DEC);
|
||||||
|
deDataBuff.put( recv3 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
byte[] deData = new byte[ deDataBuff.position() ];
|
||||||
|
System.arraycopy( deDataBuff.array(), 0, deData, 0, deData.length );
|
||||||
|
System.out.println( "DeData HEX=" + Util.bytes2HexString( deData ));
|
||||||
|
|
||||||
|
// 断言解密后和原文相同
|
||||||
|
Assert.assertArrayEquals( deData, data );
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -10,13 +10,10 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
public class LogTest {
|
public class LogTest {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SydApi.SYD_SetLogConfig("./test-log/",11);
|
Logger log = LogFactory.createLogger("./test-log/", 3);
|
||||||
// int[] a1 = {1,2,3};
|
log.error("error");
|
||||||
// Logger log = LogFactory.createLogger("./test-log/", 9);
|
log.info("info");
|
||||||
// log.error("error");
|
log.debug("debug");
|
||||||
// log.info(Arrays.toString(a1));
|
System.out.println(new byte[0].length);
|
||||||
// log.debug("debug");
|
|
||||||
// System.out.println("发送数据");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ public class PrefTest
|
|||||||
final SydApi api = new SydApi4Database();
|
final SydApi api = new SydApi4Database();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
api.SYD_Connect_Ex(
|
api.SYD_Short_Connect_Ex(
|
||||||
new String[]{ "192.168.0.200" },
|
new String[]{ "192.168.0.200" },
|
||||||
new int[]{ 8889 },
|
new int[]{ 8889 },
|
||||||
10,
|
10,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user