sydapi-java-sge-database/src/main/java/com/sunyard/sge/database/SydApi4Database.java
2022-09-05 11:26:47 +08:00

790 lines
28 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 SydApi4Database implements SydApi {
static {
SydApi.SYD_SetLogConfig("./logs/", 0);
}
private SydApi4j[] hsms = null;
private HsmLinkInfo[] linkInfos = null;
private ObjectInPool oip = null;
private boolean isShortLinkMode = false;
public SydApi4Database() {
isShortLinkMode = true;
}
public SydApi4Database(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) {
// 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) {
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_BatchDataiKeyIndex{},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 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);
}
}