@@ -14,7 +14,11 @@ import java.util.List;
* 上海黄金交易所 - 数据库加密方案
*/
public class SydApi4Database implements SydApi {
static Logger log = LogFactory . getLogger ( ) ;
static {
SydApi . SYD_SetLogConfig ( " ./logs/ " , 0 ) ;
}
private SydApi4j [ ] hsms = null ;
private boolean isShortLinkMode = false ;
@@ -28,37 +32,36 @@ public class SydApi4Database implements SydApi {
@Override
public SydApi SYD_Short_Connect_Ex ( String [ ] pcIpList , int [ ] iPortList , int iConnectTimeOut , int allDealTimeOut ) {
if ( LogFactory . iLogLevel = = 3 ) {
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 ) {
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 + + ) {
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 ) {
} catch ( Exception e ) {
// 忽略错误
log . error ( " 网络连接错误 {} {} - 忽略 " , pcIpList [ i ] , iPortList [ i ] ) ;
log . error ( " 网络连接错误 {} {} - 忽略 " , pcIpList [ i ] , iPortList [ i ] ) ;
}
}
if ( ! connected ) {
if ( ! connected ) {
// TODO
throw new SydApiException ( " 连接错误 " , - 1 ) ;
throw new SydApiException ( " 连接错误 " , - 1 ) ;
}
hsms = apis ;
isShortLinkMode = true ;
return this ;
@@ -67,7 +70,7 @@ public class SydApi4Database implements SydApi {
@Override
public void SYD_Disconnect_Ex ( ) {
if ( isShortLinkMode ) {
if ( null = = hsms ) {
if ( null = = hsms ) {
return ;
}
@@ -89,7 +92,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -99,13 +103,13 @@ public class SydApi4Database implements SydApi {
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
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 ) ;
} catch ( SydApiException e ) {
le = e ;
@@ -120,7 +124,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
@@ -131,17 +136,17 @@ public class SydApi4Database implements SydApi {
byte [ ] procData = null ;
if ( 2 = = iPkgNum | | 3 = = iPkgNum ) {
procData = Util . hexString2Bytes ( pcProcData ) ;
log . debug ( " Util.hexString2Bytes方法返回值:{} " , Arrays . toString ( procData ) ) ;
log . debug ( " Util.hexString2Bytes方法返回值:{} " , Arrays . toString ( procData ) ) ;
}
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
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 ) {
return new LongDataReturn < String , String > ( null , Util . bytes2HexString ( hash ) ) ;
} else {
@@ -160,10 +165,11 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
if ( iFlag = = Consts . ECB_DEC & & null = = pcData | | pcData . length = = 0 ) {
if ( iFlag = = Consts . ECB_DEC & & null = = pcData | | pcData . length = = 0 ) {
throw new SydApiException ( 19 ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -173,7 +179,7 @@ public class SydApi4Database implements SydApi {
String keyIndex = String . format ( " %08d " , iKeyIndex ) ;
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -203,12 +209,13 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
if ( iFlag = = Consts . ECB_DEC ) {
for ( byte [ ] pcdata : pcData ) {
if ( null = = pcData | | pcdata . length = = 0 ) {
if ( iFlag = = Consts . ECB_DEC ) {
for ( byte [ ] pcdata : pcData ) {
if ( null = = pcData | | pcdata . length = = 0 ) {
throw new SydApiException ( 19 ) ;
}
}
@@ -224,7 +231,7 @@ public class SydApi4Database implements SydApi {
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -247,10 +254,11 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
if ( iFlag = = Consts . ECB_DEC & & null = = pcData | | pcData . length = = 0 ) {
if ( iFlag = = Consts . ECB_DEC & & null = = pcData | | pcData . length = = 0 ) {
throw new SydApiException ( 19 ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -260,7 +268,7 @@ public class SydApi4Database implements SydApi {
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -293,7 +301,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ( ) ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -306,7 +315,7 @@ public class SydApi4Database implements SydApi {
}
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -333,7 +342,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -346,7 +356,7 @@ public class SydApi4Database implements SydApi {
}
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -373,7 +383,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -391,7 +402,7 @@ public class SydApi4Database implements SydApi {
SydApiException le = null ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -416,6 +427,7 @@ public class SydApi4Database implements SydApi {
} catch ( SydApiException e ) {
le = e ;
if ( e . getRetCode ( ) < 0 ) {
log . error ( " HA 重试 " ) ;
continue ;
}
throw e ;
@@ -426,7 +438,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
LongDataReturn < String , String > ret = SYD_SM4Mac_LongData ( iKeyIndex , pcData , iPkgNum , pcProcData ) ;
@@ -442,7 +455,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -450,10 +464,10 @@ public class SydApi4Database implements SydApi {
}
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 ;
for ( SydApi4j api : this . hsms ) {
if ( null = = api ) {
if ( null = = api ) {
continue ;
}
try {
@@ -473,7 +487,8 @@ public class SydApi4Database implements SydApi {
@Override
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 ) ;
}
if ( null = = this . hsms | | this . hsms . length < 1 ) {
@@ -485,7 +500,7 @@ public class SydApi4Database implements SydApi {
}
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 ) ;
}
}