diff --git a/src/main/java/com/sunyard/sge/database/ISM3.java b/src/main/java/com/sunyard/sge/database/ISM3.java index d2352f6..2360eff 100644 --- a/src/main/java/com/sunyard/sge/database/ISM3.java +++ b/src/main/java/com/sunyard/sge/database/ISM3.java @@ -1,6 +1,7 @@ package com.sunyard.sge.database; public interface ISM3 { - public void update(byte[] pcData); - public byte[] digest(); + void update(byte[] pcData); + + byte[] digest(); } diff --git a/src/main/java/com/sunyard/sge/database/ISM4Mac.java b/src/main/java/com/sunyard/sge/database/ISM4Mac.java index 84f4cdc..1d30e00 100644 --- a/src/main/java/com/sunyard/sge/database/ISM4Mac.java +++ b/src/main/java/com/sunyard/sge/database/ISM4Mac.java @@ -1,7 +1,7 @@ package com.sunyard.sge.database; public interface ISM4Mac { - public void update(byte[] pcData); - public byte[] digest(); - public boolean checkDigest(byte[] mac); + void update(byte[] pcData); + byte[] digest(); + boolean checkDigest(byte[] mac); } diff --git a/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java b/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java index af4a8a9..03f09dc 100644 --- a/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java +++ b/src/main/java/com/sunyard/sge/database/SydApiBaseFunction.java @@ -1,13 +1,9 @@ package com.sunyard.sge.database; -import com.sun.jna.ptr.IntByReference; import com.sunyard.SydApiException; import com.sunyard.proto.Util; -import com.sunyard.sge.bytes.BytesUtil; import com.sunyard.sge.database.pool.HsmLinkInfo; import com.sunyard.sge.log.LogFactory; -import com.sunyard.sge.plus.ConnectHandlerManager; -import com.sunyard.sge.plus.SydLibrary; import com.sunyard.sge.pool.ObjectInPool; import com.sunyard.util.SYMUtil; import org.apache.logging.log4j.Logger; @@ -48,8 +44,8 @@ public class SydApiBaseFunction implements SydApi { this.oip = oip; } - private void updateLastUseTime(){ - if ( null != this.oip ) { + private void updateLastUseTime() { + if (null != this.oip) { this.oip.updateLastUseTime(); } } @@ -130,10 +126,10 @@ public class SydApiBaseFunction implements SydApi { } if (null == pcData) { - throw new SydApiException("输入内容为 null",19); + throw new SydApiException("输入内容为 null", 19); } - if ( pcData.length > 1024 ) { - throw new SydApiException("输入内容为空",19); + if (pcData.length > 1024) { + throw new SydApiException("输入内容为空", 19); } SydApiException le = new SydApiException(-1); @@ -175,7 +171,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -242,7 +238,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -251,7 +247,7 @@ public class SydApiBaseFunction implements SydApi { @Override public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) { Logger log = LogFactory.getLogger(); - if ( iKeyIndex < 0 ) { + if (iKeyIndex < 0) { throw new SydApiException(0x01000001); } if (LogFactory.iLogLevel == 3) { @@ -264,11 +260,11 @@ public class SydApiBaseFunction implements SydApi { throw new SydApiException(-1); } if (Consts.ECB_ENC == iFlag) { - if ( 0 == pcData.length || pcData.length > 1024 ) { - throw new SydApiException("输入内容长度错误",0x01000001); + if (0 == pcData.length || pcData.length > 1024) { + throw new SydApiException("输入内容长度错误", 0x01000001); } } else { - if ( 0 == pcData.length || pcData.length % 16 != 0 || pcData.length > 1040 ) { + if (0 == pcData.length || pcData.length % 16 != 0 || pcData.length > 1040) { throw new SydApiException("输入密文数据长度错误", 0x01000014); } } @@ -329,7 +325,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -342,7 +338,7 @@ public class SydApiBaseFunction implements SydApi { log.debug("SYD_SM4_BatchData方法接收参数:iKeyIndex:{},pcData元素个数为:{},iFlag:{}", iKeyIndex, pcData.size(), iFlag); } - if ( null == pcData ) { + if (null == pcData) { throw new SydApiException("输入参数错误", 0x01000001); } @@ -352,13 +348,13 @@ public class SydApiBaseFunction implements SydApi { if (iFlag == Consts.ECB_DEC) { for (byte[] pcdata : pcData) { - if ( pcdata.length == 0 || pcdata.length % 16 != 0 || pcdata.length > 1040) { + if (pcdata.length == 0 || pcdata.length % 16 != 0 || pcdata.length > 1040) { throw new SydApiException(0x01000015); } } } else { for (byte[] pcdata : pcData) { - if ( pcdata.length > 1024) { + if (pcdata.length > 1024) { throw new SydApiException(19); } } @@ -371,7 +367,7 @@ public class SydApiBaseFunction implements SydApi { String[] keys = new String[iKeyIndex.length]; updateLastUseTime(); for (int i = 0; i < iKeyIndex.length; i++) { - if ( iKeyIndex[i] < 0 ) { + if (iKeyIndex[i] < 0) { throw new SydApiException(0x01000001); } keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); @@ -418,7 +414,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -431,7 +427,7 @@ public class SydApiBaseFunction implements SydApi { log.debug("SYD_SM4_LongData方法接收参数:iKeyIndex:{},pcData:{},iPkgNum:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag); } - if ( iKeyIndex < 0 ) { + if (iKeyIndex < 0) { throw new SydApiException(0x01000001); } @@ -495,7 +491,7 @@ public class SydApiBaseFunction implements SydApi { } } catch (SydApiException e) { - if (null==e){ + if (null == e) { } le = e; @@ -508,7 +504,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -532,7 +528,7 @@ public class SydApiBaseFunction implements SydApi { String[] keys = new String[iKeyIndex.length]; updateLastUseTime(); for (int i = 0; i < iKeyIndex.length; i++) { - if ( iKeyIndex[i] < 0 ) { + if (iKeyIndex[i] < 0) { throw new SydApiException(0x01000001); } keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); @@ -550,7 +546,7 @@ public class SydApiBaseFunction implements SydApi { byte[] mac = new byte[16]; byte[] buff = list.get(i); System.arraycopy(buff, buff.length - 16, mac, 0, 16); - ret.add( mac ); + ret.add(mac); } return ret; @@ -571,7 +567,7 @@ public class SydApiBaseFunction implements SydApi { byte[] mac = new byte[16]; byte[] buff = list.get(i); System.arraycopy(buff, buff.length - 16, mac, 0, 16); - ret.add( mac ); + ret.add(mac); } return ret; @@ -586,7 +582,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -662,7 +658,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -752,7 +748,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -787,7 +783,7 @@ public class SydApiBaseFunction implements SydApi { log.debug("SYD_SM4Mac_ShortData方法接收参数:iKeyIndex:{},pcData:{},iFlag:{}", iKeyIndex, Arrays.toString(pcData)); } - if ( null == pcData || pcData.length > limitDataLen ) { + if (null == pcData || pcData.length > limitDataLen) { throw new SydApiException("输入内容长度错误", 0x01000001); } @@ -818,9 +814,8 @@ public class SydApiBaseFunction implements SydApi { } throw e; } - } - else { - synchronized (api){ + } else { + synchronized (api) { try { return api.SYD_SM4Mac_ShortData(keyIndex, pcData); @@ -836,7 +831,7 @@ public class SydApiBaseFunction implements SydApi { } } } - if ( ! this.isShortLinkMode ) { + if (!this.isShortLinkMode) { this.oip.setError(); } throw le; @@ -854,8 +849,8 @@ public class SydApiBaseFunction implements SydApi { log.debug("SYD_SM4Mac_ShortData 方法接收参数:{}", iKeyIndex, Util.bytes2HexString(pcData), Util.bytes2HexString(pcMac)); } - if ( null == pcData || pcData.length > limitDataLen ) { - throw new SydApiException("输入内容超长",19); + if (null == pcData || pcData.length > limitDataLen) { + throw new SydApiException("输入内容超长", 19); } if (null == this.hsms || this.hsms.length < 1) { @@ -874,9 +869,10 @@ public class SydApiBaseFunction implements SydApi { /** * 非依赖 C 实现,仅长连接依赖 C + * * @param iEncKeyIndex:输入。加密所需要的密钥索引值 * @param iMacKeyIndex:输入。计算mac所需要的密钥索引值 - * @param pcData: 输入。计算mac所需要的数据。 + * @param pcData: 输入。计算mac所需要的数据。 * @return */ @Override @@ -893,7 +889,6 @@ public class SydApiBaseFunction implements SydApi { } - @Override public DataAndMacCheck SYD_SM4_CheckMacAndDecrypt_ShortData(int iEncKeyIndex, int iMacKeyIndex, byte[] pcData, byte[] mac) { Logger log = LogFactory.getLogger(); @@ -905,7 +900,7 @@ public class SydApiBaseFunction implements SydApi { byte[] data = null; try { data = SYD_SM4_ShortData(iEncKeyIndex, pcData, Consts.ECB_DEC); - }catch (Exception e){ + } catch (Exception e) { return new DataAndMacCheck(null, ret); } return new DataAndMacCheck(data, ret); @@ -919,11 +914,11 @@ public class SydApiBaseFunction implements SydApi { } if (null == this.hsms || null == iMacKeyIndex || null == pcData) { - throw new SydApiException("输入内容为空",0x01000001); + throw new SydApiException("输入内容为空", 0x01000001); } - if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) { + if (iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size()) { throw new SydApiException("密钥长度和数据长度不对应", 0x01000001); } @@ -935,7 +930,7 @@ public class SydApiBaseFunction implements SydApi { List enDatas = SYD_SM4_BatchData(iEncKeyIndex, pcData, Consts.ECB_ENC); List macs = SYD_SM4Mac_BatchData(iMacKeyIndex, enDatas); List ret = new ArrayList(); - for ( int i=0 ; i< enDatas.size() ; i++ ) { + for (int i = 0; i < enDatas.size(); i++) { DataAndMac dam = new DataAndMac(enDatas.get(i), macs.get(i)); ret.add(dam); } @@ -954,16 +949,16 @@ public class SydApiBaseFunction implements SydApi { return new ArrayList<>(); } - if (null == this.hsms || null == iMacKeyIndex ) { + if (null == this.hsms || null == iMacKeyIndex) { throw new SydApiException("输入内容为空", 0x01000001); } - if ( iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size() ) { + if (iEncKeyIndex.length != iMacKeyIndex.length || iEncKeyIndex.length != pcData.size()) { throw new SydApiException("密钥长度和数据长度不对应", 0x01000001); } List macs = new ArrayList<>(pcData.size()); - Listdatas = new ArrayList<>(pcData.size()); - for ( int i=0 ; i datas = new ArrayList<>(pcData.size()); + for (int i = 0; i < macs.size(); i++) { macs.add(pcData.get(i).getMac()); datas.add(pcData.get(i).getData()); } @@ -971,8 +966,8 @@ public class SydApiBaseFunction implements SydApi { boolean[] macCheck = SYD_SM4Mac_BatchData(iMacKeyIndex, datas, macs); List enData = SYD_SM4_BatchData(iEncKeyIndex, datas, Consts.ECB_DEC); - Listret = new ArrayList<>(enData.size()); - for ( int i=0 ; i ret = new ArrayList<>(enData.size()); + for (int i = 0; i < enData.size(); i++) { DataAndMacCheck damc = new DataAndMacCheck(enData.get(i), macCheck[i]); ret.add(damc); } @@ -983,7 +978,7 @@ public class SydApiBaseFunction implements SydApi { @Override public ISM4 initSM4(int iKeyIndex, int ende) { - return new SydSM4(this,iKeyIndex,ende); + return new SydSM4(this, iKeyIndex, ende); } @Override @@ -997,7 +992,6 @@ public class SydApiBaseFunction implements SydApi { } - protected static int[] toIntArray(List list) { if (null == list || list.isEmpty()) { return new int[0];