diff --git a/src/main/java/racal/sunyard/main/SydApi4j.java b/src/main/java/racal/sunyard/main/SydApi4j.java index 1ae4b6d..e1cd401 100644 --- a/src/main/java/racal/sunyard/main/SydApi4j.java +++ b/src/main/java/racal/sunyard/main/SydApi4j.java @@ -52,6 +52,7 @@ import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.NoSuchProviderException; import java.security.Security; import java.security.cert.CertificateException; @@ -60,10 +61,7 @@ import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.time.Duration; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -81,9 +79,11 @@ public class SydApi4j implements SydApi { private static boolean debug = false; - private static long sTime = System.currentTimeMillis(); +// private static long sTime = System.currentTimeMillis(); - /** --------------------------主备切换字段--------------------------------------------*/ + /** + * --------------------------主备切换字段-------------------------------------------- + */ private static String ha_ip1; private static String ha_ip2; private static int ha_port; @@ -92,16 +92,18 @@ public class SydApi4j implements SydApi { private static final AtomicBoolean usingBackup = new AtomicBoolean(false); //连接数计数器 - private static final AtomicInteger connectionCounter = new AtomicInteger(0); +// private static final AtomicInteger connectionCounter = new AtomicInteger(0); private final Object reqLock = new Object(); - /** --------------------------主备切换字段--------------------------------------------*/ + /** + * --------------------------主备切换字段-------------------------------------------- + */ // 网络失败时重试次数 private String ip; private int port; - private String pwd; + // private String pwd; private int connectTimeout; private int readTimeout; private int retryWhenNetError = 0; @@ -133,12 +135,11 @@ public class SydApi4j implements SydApi { /** * 简单双机热备模式 * - * @param ip1 - * @param ip2 - * @param port - * @param timeout - * todo 目前主备切换只适配了安徽农信接口 - * @return + * @param ip1:主机IP地址 + * @param ip2:备机IP地址 + * @param port:端口号 + * @param timeout:超时时间(毫秒) todo 目前主备切换只适配了安徽农信接口 + * @return:成功返回0,失败返回错误码 */ public static int SYD_Set_Parameter_HA( String ip1, @@ -171,8 +172,8 @@ public class SydApi4j implements SydApi { ProtoQN proto = new ProtoQN(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -197,7 +198,11 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + +// ret = (Map) ret.get("data"); PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -223,8 +228,8 @@ public class SydApi4j implements SydApi { sk = SYMUtil.genPrivateKeyMark(userId, nKeyType == 0 ? "000" : "001", "00000001"); } - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -245,7 +250,10 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { @@ -268,8 +276,8 @@ public class SydApi4j implements SydApi { sk = SYMUtil.genPrivateKeyMark(userId, nKeyType == 0 ? "000" : "001", "00000001"); } - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -290,7 +298,10 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { @@ -301,7 +312,7 @@ public class SydApi4j implements SydApi { String skv = ((PacketSection) ret.get("sk")).getString(); RetWrap r = new RetWrap(); // r.put("publicKey", "03420004" + pkv); - r.put("publicKey", Util.bytes2HexString(pkv)); + r.put("publicKey", Util.bytes2HexString(pkv)); r.put("privateKey", skv); return r; } @@ -336,8 +347,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -392,8 +403,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -432,8 +443,8 @@ public class SydApi4j implements SydApi { @Override public byte[] transPinFrOneKeyToAno(String srcKey, String desKey, int sca, String sPin, String primaryAccount) { ProtoCB proto = new ProtoCB(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -458,20 +469,22 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { - byte[] pinBlock = ((PacketSection) ret.get("dPin")).getBytes(); - return pinBlock; + return ((PacketSection) ret.get("dPin")).getBytes(); } } } @@ -508,8 +521,8 @@ public class SydApi4j implements SydApi { byte[] sign = SYMUtil.base64SignAsDerSign(base64Sign); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -530,21 +543,17 @@ public class SydApi4j implements SydApi { byte[] v = new byte[2]; bb.get(v, 0, v.length); String a = Util.bytes2HexString(v); - if ("3030".equals(a)) { - return true; - } else { - return false; - } + return "3030".equals(a); } - public byte[] SYD_NakedSignAll(int rFlag,byte[] orgData, String sCertDN){ + public byte[] SYD_NakedSignAll(int rFlag, byte[] orgData, String sCertDN) { try { - String cert = getCertByDn(sCertDN,0); + String cert = getCertByDn(sCertDN, 0); boolean sm2Cert = CertUtil.isSM2Cert(cert); - if(!sm2Cert){//RSA - return SYD_NakedSign(rFlag,orgData,sCertDN); - }else { - return SYD_NakedSign(orgData,sCertDN); + if (!sm2Cert) {//RSA + return SYD_NakedSign(rFlag, orgData, sCertDN); + } else { + return SYD_NakedSign(orgData, sCertDN); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); @@ -552,14 +561,14 @@ public class SydApi4j implements SydApi { } - public boolean SYD_NakedVerifyAll(int rFlag,byte[] orgData,byte[] sign, String sCertDN){ + public boolean SYD_NakedVerifyAll(int rFlag, byte[] orgData, byte[] sign, String sCertDN) { try { - String cert = getCertByDn(sCertDN,0); + String cert = getCertByDn(sCertDN, 0); boolean sm2Cert = CertUtil.isSM2Cert(cert); - if(!sm2Cert){//RSA - return SYD_NakedVerify(rFlag,orgData,sign,sCertDN); - }else { - return SYD_NakedVerify(orgData,sign,sCertDN); + if (!sm2Cert) {//RSA + return SYD_NakedVerify(rFlag, orgData, sign, sCertDN); + } else { + return SYD_NakedVerify(orgData, sign, sCertDN); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); @@ -567,22 +576,18 @@ public class SydApi4j implements SydApi { } - public boolean SYD_NakedVerifyAll4NX(String cert,int rFlag,byte[] orgData,byte[] sign, String sCertDN){ + public boolean SYD_NakedVerifyAll4NX(String cert, int rFlag, byte[] orgData, byte[] sign, String sCertDN) { boolean verify = false; - try { - //先导入证书 - importCert(cert.getBytes(),null); - if( rFlag == 4 ){//SM2 国密算法 - verify = SYD_NakedVerify(orgData,sign,sCertDN); - }else { - verify = SYD_NakedVerify(rFlag,orgData,sign,sCertDN); - } - //删除导入的证书 - deleteCert(sCertDN); - return verify; - } catch ( SydApiException e) { - throw e; + //先导入证书 + importCert(cert.getBytes(), null); + if (rFlag == 4) {//SM2 国密算法 + verify = SYD_NakedVerify(orgData, sign, sCertDN); + } else { + verify = SYD_NakedVerify(rFlag, orgData, sign, sCertDN); } + //删除导入的证书 + deleteCert(sCertDN); + return verify; } @@ -591,89 +596,88 @@ public class SydApi4j implements SydApi { if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - return orgData.length < 4096 ? this.SYD_NakedSign(sCertDN, orgData, (byte[])null) : this.SYD_NakedSign_OuterHash(orgData, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedSign(sCertDN, orgData, null) : this.SYD_NakedSign_OuterHash(orgData, sCertDN); } } - public void QRCodeSign(String orgData, String sCertDN,String filepath) { - byte[] sign = null; + public void QRCodeSign(String orgData, String sCertDN, String filepath) { + byte[] sign; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); } - QRCodeUtil.genQRCode(orgData+"+"+Util.bytes2HexString(sign),filepath); + QRCodeUtil.genQRCode(orgData + "+" + Util.bytes2HexString(sign), filepath); } - public void QRCodeSignRSA(int rFlag, String orgData, String sCertDN,String filepath) { - byte[] sign = null; + public void QRCodeSignRSA(int rFlag, String orgData, String sCertDN, String filepath) { + byte[] sign; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag,sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(rFlag,orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag, sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(rFlag, orgData.getBytes(), sCertDN); } - QRCodeUtil.genQRCode(orgData+"+"+Util.bytes2HexString(sign),filepath); + QRCodeUtil.genQRCode(orgData + "+" + Util.bytes2HexString(sign), filepath); } - public boolean QRCodeVer( String sCertDN,String filepath) { + public boolean QRCodeVer(String sCertDN, String filepath) { String s = QRCodeUtil.readQRCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); } - public boolean QRCodeVerRSA( int rFlag,String sCertDN,String filepath) { + public boolean QRCodeVerRSA(int rFlag, String sCertDN, String filepath) { String s = QRCodeUtil.readQRCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag,sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(rFlag,orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(rFlag, orgData, sign, sCertDN); } - - public void BarCodeSign(String orgData, String sCertDN,String filepath) { - byte[] sign = null; + public void BarCodeSign(String orgData, String sCertDN, String filepath) { + byte[] sign; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(orgData.getBytes(), sCertDN); } - QRCodeUtil.genBarCode(orgData+"+"+Util.bytes2HexString(sign),filepath); + QRCodeUtil.genBarCode(orgData + "+" + Util.bytes2HexString(sign), filepath); } - public void BarCodeSignRSA(int rFlag, String orgData, String sCertDN,String filepath) { - byte[] sign = null; + public void BarCodeSignRSA(int rFlag, String orgData, String sCertDN, String filepath) { + byte[] sign; if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag,sCertDN, orgData.getBytes(), (byte[])null) : this.SYD_NakedSign_OuterHash(rFlag,orgData.getBytes(), sCertDN); + sign = orgData.length() < 4096 ? this.SYD_NakedSign(rFlag, sCertDN, orgData.getBytes(), null) : this.SYD_NakedSign_OuterHash(rFlag, orgData.getBytes(), sCertDN); } - QRCodeUtil.genBarCode(orgData+"+"+Util.bytes2HexString(sign),filepath); + QRCodeUtil.genBarCode(orgData + "+" + Util.bytes2HexString(sign), filepath); } - public boolean BarCodeVer( String sCertDN,String filepath) { + public boolean BarCodeVer(String sCertDN, String filepath) { String s = QRCodeUtil.readBarCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); } - public boolean BarCodeVerRSA( int rFlag,String sCertDN,String filepath) { + public boolean BarCodeVerRSA(int rFlag, String sCertDN, String filepath) { String s = QRCodeUtil.readBarCode(filepath); String[] split = s.split("\\+"); byte[] orgData = split[0].getBytes(); byte[] sign = Util.hexString2Bytes(split[1]); - return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag,sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(rFlag,orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(rFlag, orgData, sign, sCertDN); } public byte[] SYD_NakedSign(String sCertDN, byte[] orgData, byte[] hash) { @@ -684,16 +688,25 @@ public class SydApi4j implements SydApi { byte[] dn = new byte[256]; byte[] s = null; +// try { +// // sCertDN = DnUtil.verifyDn(sCertDN); +// s = sCertDN.getBytes("GBK"); +// } catch (UnsupportedEncodingException var16) { +// var16.printStackTrace(); +// } try { - // sCertDN = DnUtil.verifyDn(sCertDN); s = sCertDN.getBytes("GBK"); } catch (UnsupportedEncodingException var16) { var16.printStackTrace(); } - System.arraycopy(s, 0, dn, 0, s.length); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + if (s != null) { + System.arraycopy(s, 0, dn, 0, s.length); // ✅ 只有这一个安全的调用 + } + + System.arraycopy(s != null ? s : new byte[0], 0, dn, 0, s != null ? s.length : 0); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); data.put("signatureDataType$option", 1); @@ -712,44 +725,45 @@ public class SydApi4j implements SydApi { Packet p = proto.parse(bb); Map ret = p.toMap(); - if (!Arrays.equals(sn.getSn().array(), ((PacketSection)ret.get("header")).getBytes())) { + if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); - } - else - { - ret = (Map)ret.get("data"); - PacketSection error = (PacketSection)ret.get("error"); + } else { +// ret = (Map)ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { - return ((PacketSection)ret.get("signature")).getBytes(); + return ((PacketSection) ret.get("signature")).getBytes(); } } } private ByteBuffer sendAndGetResponse(ByteBuffer data) { - ByteBuffer readData = null; + ByteBuffer readData; int maxRetries = 2; int retryCount = 0; while (retryCount < maxRetries) { try { - synchronized(reqLock) { + synchronized (reqLock) { byte[] sentData = this.syncSend(data); readData = this.syncRead(sentData); return readData; } } catch (SydApiException e) { handleNetworkFailure(e, retryCount, maxRetries); // 统一处理网络故障 - retryCount ++; + retryCount++; } } throw new SydApiException("加密机连接失败,主备节点均不可用", -1); } public byte[] SYD_NakedSign_OuterHash(byte[] orgData, String sCertDN) { - // sCertDN = DnUtil.verifyDn(sCertDN); - String pk = null; + // sCertDN = DnUtil.verifyDn(sCertDN); + String pk; try { pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); @@ -759,12 +773,12 @@ public class SydApi4j implements SydApi { } byte[] hash = this.SM3Hash(pk, orgData); - return this.SYD_NakedSign(sCertDN, (byte[])null, hash); + return this.SYD_NakedSign(sCertDN, null, hash); } - public byte[] SYD_NakedSign_OuterHash(int rFlag,byte[] orgData, String sCertDN) { + public byte[] SYD_NakedSign_OuterHash(int rFlag, byte[] orgData, String sCertDN) { byte[] hash = this.itHash(rFlag, orgData);// HASH类型。0: SHA1,1: SHA256,2: MD5 - return this.SYD_NakedSign(rFlag,sCertDN, (byte[])null, hash); + return this.SYD_NakedSign(rFlag, sCertDN, null, hash); } @@ -772,15 +786,15 @@ public class SydApi4j implements SydApi { if (orgData == null) { throw new IllegalArgumentException("输入数据为 null"); } else { - return orgData.length < 4096 ? this.SYD_NakedSign(rFlag,sCertDN, orgData, (byte[])null) : this.SYD_NakedSign_OuterHash(rFlag,orgData, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedSign(rFlag, sCertDN, orgData, null) : this.SYD_NakedSign_OuterHash(rFlag, orgData, sCertDN); } } - public byte[] SYD_NakedSign(int rFlag, String sCertDN,byte[] orgData,byte[] hash) { + public byte[] SYD_NakedSign(int rFlag, String sCertDN, byte[] orgData, byte[] hash) { Proto8003 proto = new Proto8003(); PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); byte[] dn = new byte[256]; byte[] s = null; @@ -790,27 +804,26 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); if (rFlag == 1) { // MD5 data.put("rFlag", 'M'); - } - else if (rFlag == 2) { + } else if (rFlag == 2) { // SHA-1 data.put("rFlag", 'S'); - } - else if (rFlag == 3) { + } else if (rFlag == 3) { // SHA-256 data.put("rFlag", 'R'); - }else { - throw new SydApiException("不支持的摘要算法",10001); + } else { + throw new SydApiException("不支持的摘要算法", 10001); } data.put("signatureDataType$option", 1); data.put("DN", dn); @@ -832,7 +845,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -844,13 +860,13 @@ public class SydApi4j implements SydApi { } public boolean SYD_NakedVerify(byte[] orgData, byte[] sign, String sCertDN) { - return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); + return orgData.length < 4096 ? this.SYD_NakedVerify(sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(orgData, sign, sCertDN); } public boolean SYD_NakedVerify(String sCertDN, byte[] sign, byte[] orgData, byte[] hash) { Proto8004 proto = new Proto8004(); PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); sign = ASN1Util.alignSignDer(sign); byte[] dn = new byte[256]; @@ -863,8 +879,9 @@ public class SydApi4j implements SydApi { var18.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -876,7 +893,7 @@ public class SydApi4j implements SydApi { data.put("signature", sign); data.put("returnSign$option", 0); ByteBuffer bb = proto.rend(packet); - + // synchronized(this) { // byte[] b = this.syncSend(bb); // bb = this.syncRead(b); @@ -889,12 +906,15 @@ public class SydApi4j implements SydApi { // throw new SydApiException("包序号不匹配", -4); // } else { - ret = (Map)ret.get("data"); - PacketSection error = (PacketSection)ret.get("error"); +// ret = (Map)ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e.toString()); + println("验签失败=" + e); log.info("验签失败:{}", e.toString()); return false; } else { @@ -904,8 +924,8 @@ public class SydApi4j implements SydApi { } public boolean SYD_NakedVerify_OuterHash(byte[] orgData, byte[] sign, String sCertDN) { - // sCertDN = DnUtil.verifyDn(sCertDN); - String pk = null; + // sCertDN = DnUtil.verifyDn(sCertDN); + String pk; try { pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); @@ -918,20 +938,20 @@ public class SydApi4j implements SydApi { return this.SYD_NakedVerify(sCertDN, sign, orgData, hash); } - public boolean SYD_NakedVerify_OuterHash(int rFlag,byte[] orgData, byte[] sign, String sCertDN) { + public boolean SYD_NakedVerify_OuterHash(int rFlag, byte[] orgData, byte[] sign, String sCertDN) { byte[] hash = this.itHash(rFlag, orgData); - return this.SYD_NakedVerify(rFlag,sCertDN, sign, orgData, hash); + return this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, hash); } - public boolean SYD_NakedVerify(int rFlag,byte[] orgData, byte[] sign, String sCertDN) { - return orgData.length < 4096 ? this.SYD_NakedVerify( rFlag,sCertDN, sign, orgData, (byte[])null) : this.SYD_NakedVerify_OuterHash( rFlag,orgData, sign, sCertDN); + public boolean SYD_NakedVerify(int rFlag, byte[] orgData, byte[] sign, String sCertDN) { + return orgData.length < 4096 ? this.SYD_NakedVerify(rFlag, sCertDN, sign, orgData, null) : this.SYD_NakedVerify_OuterHash(rFlag, orgData, sign, sCertDN); } - public boolean SYD_NakedVerify(int rFlag, String sCertDN,byte[] sign, byte[] orgData, byte[] hashData) { + public boolean SYD_NakedVerify(int rFlag, String sCertDN, byte[] sign, byte[] orgData, byte[] hashData) { Proto8004 proto = new Proto8004(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -943,10 +963,11 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -984,7 +1005,10 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -999,7 +1023,7 @@ public class SydApi4j implements SydApi { public String sm3AndDetachedSign(byte[] orgData, String publicKey, String sCertDN) { byte[] hash = SM3Hash(publicKey, orgData); - System.out.println("hash为:"+Util.bytes2HexString(hash)); + System.out.println("hash为:" + Util.bytes2HexString(hash)); return detachedSign(DATA_HASH, hash, sCertDN); } @@ -1009,8 +1033,8 @@ public class SydApi4j implements SydApi { public RetWrap genComposeKey(String[] keys) { ProtoGY proto = new ProtoGY(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); int len = 0; for (String key : keys) { @@ -1045,13 +1069,16 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -1076,8 +1103,8 @@ public class SydApi4j implements SydApi { public RetWrap genComposeKey(String keyType, char keySolution, String keyClearPattern) { ProtoAA proto = new ProtoAA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -1093,13 +1120,16 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -1121,7 +1151,7 @@ public class SydApi4j implements SydApi { @Override public boolean sm3AndDetachedVerify(byte[] orgData, String publicKey, String sign) { byte[] hash = SM3Hash(publicKey, orgData); - System.out.println("验签hash为:"+Util.bytes2HexString(hash)); + System.out.println("验签hash为:" + Util.bytes2HexString(hash)); return detachedVerify(DATA_HASH, hash, sign); } @@ -1142,23 +1172,24 @@ public class SydApi4j implements SydApi { @Override public String detachedSign(int dataType, byte[] orgData, String sCertDN) { Proto8005 proto = new Proto8005(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; byte[] s = null; try { - // sCertDN = verifyDn(sCertDN); + // sCertDN = verifyDn(sCertDN); s = sCertDN.getBytes("GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1187,7 +1218,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -1196,8 +1230,7 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); } - String signature = ((PacketSection) ret.get("signature")).getString(); - return signature; + return ((PacketSection) ret.get("signature")).getString(); } @@ -1205,11 +1238,11 @@ public class SydApi4j implements SydApi { @Override public boolean detachedVerify(int dataType, byte[] orgData, String sign) { Proto8006 proto = new Proto8006(); - // 填充数据 - HashMap packet = new HashMap(); + // 填充数据 + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1242,54 +1275,51 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e.toString()); + println("验签失败=" + e); return false; } return true; } - public boolean detachedVerifyAll(int rFlag,byte[] orgData, String sign){ + public boolean detachedVerifyAll(int rFlag, byte[] orgData, String sign) { try { boolean sm2Cert = CertUtil.isSignedBySM2Cert(sign); - if(!sm2Cert){//RSA - return detachedVerifyRSA(rFlag,orgData,sign); - }else { - if( orgData.length<4096 ){ - return detachedVerify(DATA_ORIGIN,orgData,sign); - }else { + if (!sm2Cert) {//RSA + return detachedVerifyRSA(rFlag, orgData, sign); + } else { + if (orgData.length < 4096) { + return detachedVerify(DATA_ORIGIN, orgData, sign); + } else { RetWrap retWrap = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); - byte[] pk =(byte[]) retWrap.get("pk"); + byte[] pk = (byte[]) retWrap.get("pk"); byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); - return detachedVerify(DATA_HASH,hash,sign); + return detachedVerify(DATA_HASH, hash, sign); } } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } catch (CertificateException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (NoSuchProviderException e) { + } catch (CertificateException | IOException | NoSuchProviderException e) { throw new RuntimeException(e); } } - public boolean detachedVerify(byte[] orgData, String sign){ + public boolean detachedVerify(byte[] orgData, String sign) { try { - if( orgData.length<4096 ){ - return detachedVerify(DATA_ORIGIN,orgData,sign); - }else { + if (orgData.length < 4096) { + return detachedVerify(DATA_ORIGIN, orgData, sign); + } else { RetWrap retWrap = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); - byte[] pk =(byte[]) retWrap.get("pk"); + byte[] pk = (byte[]) retWrap.get("pk"); byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); - return detachedVerify(DATA_HASH,hash,sign); + return detachedVerify(DATA_HASH, hash, sign); } } catch (IOException e) { throw new RuntimeException(e); @@ -1297,8 +1327,8 @@ public class SydApi4j implements SydApi { } - public String getCertBySign(String sign){ - RetWrap retWrap = null; + public String getCertBySign(String sign) { + RetWrap retWrap; try { retWrap = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); } catch (IOException e) { @@ -1306,9 +1336,9 @@ public class SydApi4j implements SydApi { } String cert = (String) retWrap.get("cert"); String pemCert; - if(cert.contains("-----BEGIN CERTIFICATE-----")){ - pemCert =cert; - }else { + if (cert.contains("-----BEGIN CERTIFICATE-----")) { + pemCert = cert; + } else { String cert_begin = "-----BEGIN CERTIFICATE-----\n"; String end_cert = "\n-----END CERTIFICATE-----"; pemCert = cert_begin + cert + end_cert; @@ -1317,48 +1347,48 @@ public class SydApi4j implements SydApi { } - public HashMap getCertTime(String dn){ - String cert = null; + public HashMap getCertTime(String dn) { + String cert; try { cert = getCertByDn(dn); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } - String pemCert; - if(cert.contains("-----BEGIN CERTIFICATE-----")){ - pemCert =cert; - }else { - String cert_begin = "-----BEGIN CERTIFICATE-----\n"; - String end_cert = "\n-----END CERTIFICATE-----"; - pemCert = cert_begin + cert + end_cert; - } - X509Certificate certificate = null; +// String pemCert; +// if(cert.contains("-----BEGIN CERTIFICATE-----")){ +// pemCert =cert; +// }else { +// String cert_begin = "-----BEGIN CERTIFICATE-----\n"; +// String end_cert = "\n-----END CERTIFICATE-----"; +// pemCert = cert_begin + cert + end_cert; +// } + X509Certificate certificate; try { certificate = CertUtil.convertToX509Cert(cert); } catch (Exception e) { - throw new SydApiException( 0xC101, e ); + throw new SydApiException(0xC101, e); } SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd"); String afterDate = outputFormat.format(certificate.getNotAfter()); String beforeDate = outputFormat.format(certificate.getNotBefore()); - HashMap hashMap = new HashMap(); - hashMap.put("after",afterDate); - hashMap.put("before",beforeDate); - return hashMap; + HashMap hashMap = new HashMap<>(); + hashMap.put("after", afterDate); + hashMap.put("before", beforeDate); + return hashMap; } - public String importCertAndGetDN(String cert){ - RetWrap retWrap = importCert(cert.getBytes(),null); + public String importCertAndGetDN(String cert) { + RetWrap retWrap = importCert(cert.getBytes(), null); String dn = null; - int isImport= (Integer) retWrap.get("isImport"); - if(isImport == 0) { - X509Certificate certificate = null; + int isImport = (Integer) retWrap.get("isImport"); + if (isImport == 0) { + X509Certificate certificate; try { certificate = CertUtil.convertToX509Cert(cert); } catch (Exception e) { - throw new SydApiException( 0xC101, e ); + throw new SydApiException(0xC101, e); } dn = certificate.getSubjectDN().toString(); } @@ -1395,7 +1425,7 @@ public class SydApi4j implements SydApi { } String dn = null; String serialNumber = null; - X509Certificate certificate = null; + X509Certificate certificate; try { certificate = CertUtil.convertToX509Cert(cert); dn = certificate.getSubjectDN().toString(); @@ -1417,7 +1447,8 @@ public class SydApi4j implements SydApi { .map(Object::toString) .collect(Collectors.joining(",")) + "]"; String message = "支付签名证书导入失败!签名服务器地址:" + result + "| 证书对应行号:[" + bkCode + "]"; - Map map = gson.fromJson(snmpConfig, new TypeToken>() {}.getType()); + Map map = gson.fromJson(snmpConfig, new TypeToken>() { + }.getType()); try (SnmpTrapSender sender = new SnmpTrapSender(map)) { sender.sendTrap(message, 2); log.info("snmp发送告警消息:{}", message); @@ -1450,83 +1481,70 @@ public class SydApi4j implements SydApi { String[] addressParts = address.split(":"); String ip = addressParts[0]; int port = Integer.parseInt(addressParts[1]); - String dn = null; + String dn; ReentrantLock ipLock = IpLockUtils.getLockForIp(address); + ipLock.lock(); + SydApi4j sydApi4j = null; try { - ipLock.lock(); - SydApi4j sydApi4j = null; - try { - //创建sydapi4j连接对象 - sydApi4j = (SydApi4j) new SydApi4j().connect(ip, port, null, timeout); - //先查询 + //创建sydapi4j连接对象 + sydApi4j = (SydApi4j) new SydApi4j().connect(ip, port, null, timeout); + //先查询 - //导入证书 - dn = sydApi4j.importCertAndGetDN(cert); - } catch (Exception e) { - throw e; - } finally { - if (sydApi4j != null) { - sydApi4j.disconnect(); - } - ipLock.unlock(); + //导入证书 + dn = sydApi4j.importCertAndGetDN(cert); + } finally { + if (sydApi4j != null) { + sydApi4j.disconnect(); } - } catch (Exception e) { - throw e; + ipLock.unlock(); } return dn; } - public boolean attachedVerifyAll(int rFlag, String sign){ + public boolean attachedVerifyAll(int rFlag, String sign) { try { boolean sm2Cert = CertUtil.isSignedBySM2Cert(sign); - if(!sm2Cert){//RSA - return attachedVerifyRSA(rFlag,sign); - }else { - RetWrap ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); - byte[] orgData = (byte[]) ret.get("orgData"); - String detsign = (String) ret.get("sign"); - if( orgData.length<4096 ){ - return attachedVerify(sign); - }else { - byte[] pk =(byte[]) ret.get("pk"); - byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); - return detachedVerify(DATA_HASH,hash,detsign); + if (!sm2Cert) {//RSA + return attachedVerifyRSA(rFlag, sign); + } else { + RetWrap ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); + byte[] orgData = (byte[]) ret.get("orgData"); + String detsign = (String) ret.get("sign"); + if (orgData.length < 4096) { + return attachedVerify(sign); + } else { + byte[] pk = (byte[]) ret.get("pk"); + byte[] hash = this.SM3Hash(Util.bytes2HexString(pk), orgData); + return detachedVerify(DATA_HASH, hash, detsign); } } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } catch (CertificateException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (NoSuchProviderException e) { + } catch (CertificateException | IOException | NoSuchProviderException e) { throw new RuntimeException(e); } } - public boolean detachedVerifyRSA(int rFlag,byte[] orgData, String sign) { - boolean deVerify =false; - if(orgData.length<4096){ - deVerify= detachedVerify(rFlag,DATA_ORIGIN,orgData,sign); - }else { + public boolean detachedVerifyRSA(int rFlag, byte[] orgData, String sign) { + boolean deVerify = false; + if (orgData.length < 4096) { + deVerify = detachedVerify(rFlag, DATA_ORIGIN, orgData, sign); + } else { //RetWrap hashData = countHash(0, 0, orgData, null); byte[] hashData = itHash(rFlag, orgData); - deVerify= this.detachedVerify(rFlag, DATA_HASH, hashData, sign); + deVerify = this.detachedVerify(rFlag, DATA_HASH, hashData, sign); } return deVerify; } - @Override public boolean detachedVerify(int rFlag, int dataType, byte[] orgData, String sign) { Proto8006 proto = new Proto8006(); // 填充数据 - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); if (rFlag == 1) {//RSA证书 @@ -1573,12 +1591,15 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e.toString()); + println("验签失败=" + e); return false; } @@ -1591,8 +1612,8 @@ public class SydApi4j implements SydApi { Proto8006 proto = new Proto8006(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -1621,7 +1642,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -1645,8 +1669,8 @@ public class SydApi4j implements SydApi { Proto8025 proto = new Proto8025(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -1676,14 +1700,13 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); - if ((0 != (be[0] ^ be[1]))) { - - return false; - } - return true; + return 0 == (be[0] ^ be[1]); } @Override @@ -1704,22 +1727,22 @@ public class SydApi4j implements SydApi { } - public String detachedSignAll(int rFlag,byte[] orgData, String sCertDN){ + public String detachedSignAll(int rFlag, byte[] orgData, String sCertDN) { try { // String cert = getCertByDn(sCertDN,0); // boolean sm2Cert = CertUtil.isSM2Cert(cert); - if(rFlag<4){//RSA - return detachedSignRSA(rFlag,orgData,sCertDN); - }else { - if (orgData.length<4096){ - return detachedSign(DATA_ORIGIN,orgData,sCertDN); - }else { - String pk = null; + if (rFlag < 4) {//RSA + return detachedSignRSA(rFlag, orgData, sCertDN); + } else { + if (orgData.length < 4096) { + return detachedSign(DATA_ORIGIN, orgData, sCertDN); + } else { + String pk; pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); System.out.println(pk); byte[] hash = this.SM3Hash(pk, orgData); - System.out.println("hash为:"+Util.bytes2HexString(hash)); - return detachedSign(DATA_HASH,hash,sCertDN); + System.out.println("hash为:" + Util.bytes2HexString(hash)); + return detachedSign(DATA_HASH, hash, sCertDN); } } } catch (UnsupportedEncodingException e) { @@ -1728,43 +1751,41 @@ public class SydApi4j implements SydApi { } - public String detachedSign(byte[] orgData, String sCertDN){ + public String detachedSign(byte[] orgData, String sCertDN) { try { - if (orgData.length<4096){ - return detachedSign(DATA_ORIGIN,orgData,sCertDN); - }else { - String pk = null; + if (orgData.length < 4096) { + return detachedSign(DATA_ORIGIN, orgData, sCertDN); + } else { + String pk; pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); System.out.println(pk); byte[] hash = this.SM3Hash(pk, orgData); - System.out.println("hash为:"+Util.bytes2HexString(hash)); - return detachedSign(DATA_HASH,hash,sCertDN); + System.out.println("hash为:" + Util.bytes2HexString(hash)); + return detachedSign(DATA_HASH, hash, sCertDN); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } - public String attachedSignAll(int rFlag,byte[] orgData, String sCertDN){ + public String attachedSignAll(int rFlag, byte[] orgData, String sCertDN) { try { - String cert = getCertByDn(sCertDN,0); + String cert = getCertByDn(sCertDN, 0); boolean sm2Cert = CertUtil.isSM2Cert(cert); - if(!sm2Cert){//RSA - return attachedSignRSA(rFlag,orgData,sCertDN); - }else { - if (orgData.length<4096){ + if (!sm2Cert) {//RSA + return attachedSignRSA(rFlag, orgData, sCertDN); + } else { + if (orgData.length < 4096) { String sign = detachedSign(DATA_ORIGIN, orgData, sCertDN); return ASN1Util.addOriginDataToPKCS7Sign(orgData, sign); - }else { - String pk = null; + } else { + String pk; pk = this.SYD_SM2_Get_Dn_PublicKey_HA(sCertDN.getBytes("GBK"), 0); byte[] hash = this.SM3Hash(pk, orgData); String sign = detachedSign(DATA_HASH, hash, sCertDN); return ASN1Util.addOriginDataToPKCS7Sign(orgData, sign); } } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } @@ -1774,22 +1795,21 @@ public class SydApi4j implements SydApi { //人行大数据不指定摘要算法 public String detachedSignRSA(int rFlag, byte[] orgData, String sCertDN) { - String sign =null; - if(orgData.length<4096){ - sign = detachedSign(rFlag,DATA_ORIGIN,orgData,sCertDN); - }else{ - // RetWrap hashData = countHash(0, 0, orgData, null); + String sign; + if (orgData.length < 4096) { + sign = detachedSign(rFlag, DATA_ORIGIN, orgData, sCertDN); + } else { + // RetWrap hashData = countHash(0, 0, orgData, null); byte[] hash = itHash(rFlag, orgData); - sign = detachedSign(rFlag,DATA_HASH, hash, sCertDN); + sign = detachedSign(rFlag, DATA_HASH, hash, sCertDN); } return sign; } - public String detachedSign(int rFlag, int dataType, byte[] orgData, String sCertDN) { Proto8005 proto = new Proto8005(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -1801,11 +1821,12 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -1851,7 +1872,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -1899,8 +1923,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -1923,7 +1947,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -1955,14 +1982,14 @@ public class SydApi4j implements SydApi { Proto8008 proto = new Proto8008(); // 填充数据 - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - data.put("dataFlag",Util.slenBytes(dataFlag,4)); + data.put("dataFlag", Util.slenBytes(dataFlag, 4)); String sqlite3 = "1 = 1 "; data.put("sqlite3", sqlite3); @@ -1983,7 +2010,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -1998,7 +2028,7 @@ public class SydApi4j implements SydApi { PacketSection dataAttrFlag = (PacketSection) ret.get("dataAttrFlag"); PacketSection realCertNum = (PacketSection) ret.get("realCertNum"); try { - r.put("dataFlag",Util.smallLen(resDataFlag.getBytes()) ); + r.put("dataFlag", Util.smallLen(resDataFlag.getBytes())); r.put("certAllNum", Util.smallLen(certAllNum.getBytes())); r.put("dataAttrFlag", Util.smallLen(dataAttrFlag.getBytes())); r.put("realCertNum", Util.smallLen(realCertNum.getBytes())); @@ -2007,7 +2037,7 @@ public class SydApi4j implements SydApi { } r.put("certLenMax", Integer.parseInt(Util.bytes2HexString(Util.toArray(bb)).substring(60, 62), 16)); - List certs = new ArrayList(); + List certs = new ArrayList<>(); while (bb.limit() - bb.position() >= 128) { byte[] cert = new byte[128]; bb.get(cert); @@ -2016,7 +2046,7 @@ public class SydApi4j implements SydApi { // r.put("certDNs",certs.toArray(new String[0]) ); - r.put("certDNs",certs); + r.put("certDNs", certs); return r; } @@ -2025,36 +2055,39 @@ public class SydApi4j implements SydApi { @Override public List getAllCert() throws UnsupportedEncodingException { - ArrayList m_certs =new ArrayList(); //查出证书总数,一次次的拿出来 - RetWrap allCert =getAllCert(0); - Integer allnum =(Integer) allCert.get("certAllNum");//总数 - List certs = (List)allCert.get("certDNs");//部分证书列表 - m_certs.addAll(certs); - int loop = allnum%16;//循环次数 + RetWrap allCert = getAllCert(0); + Integer allnum = (Integer) allCert.get("certAllNum");//总数 +// List certs = (List)allCert.get("certDNs");//部分证书列表 + @SuppressWarnings("unchecked") + List certs = (List) allCert.get("certDNs"); + ArrayList m_certs = new ArrayList<>(certs); + int loop = allnum % 16;//循环次数 for (int i = 1; i < loop; i++) { - RetWrap partCert= getAllCert(16*i); - List partcerts = (List)partCert.get("certDNs"); + RetWrap partCert = getAllCert(16 * i); +// List partcerts = (List)partCert.get("certDNs"); + @SuppressWarnings("unchecked") + List partcerts = (List) partCert.get("certDNs"); m_certs.addAll(partcerts); } return m_certs; } - + // 测试通过 @Override - public RetWrap getAllCert(int dataFlag,String sqlite) throws UnsupportedEncodingException { + public RetWrap getAllCert(int dataFlag, String sqlite) throws UnsupportedEncodingException { Proto8008 proto = new Proto8008(); // 填充数据 - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - data.put("dataFlag",Util.slenBytes(dataFlag,4)); + data.put("dataFlag", Util.slenBytes(dataFlag, 4)); data.put("sqlite3", sqlite); @@ -2074,7 +2107,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -2089,7 +2124,7 @@ public class SydApi4j implements SydApi { PacketSection dataAttrFlag = (PacketSection) ret.get("dataAttrFlag"); PacketSection realCertNum = (PacketSection) ret.get("realCertNum"); try { - r.put("dataFlag",Util.smallLen(resDataFlag.getBytes()) ); + r.put("dataFlag", Util.smallLen(resDataFlag.getBytes())); r.put("certAllNum", Util.smallLen(certAllNum.getBytes())); r.put("dataAttrFlag", Util.smallLen(dataAttrFlag.getBytes())); r.put("realCertNum", Util.smallLen(realCertNum.getBytes())); @@ -2098,7 +2133,7 @@ public class SydApi4j implements SydApi { } r.put("certLenMax", Integer.parseInt(Util.bytes2HexString(Util.toArray(bb)).substring(60, 62), 16)); - List certs = new ArrayList(); + List certs = new ArrayList<>(); while (bb.limit() - bb.position() >= 128) { byte[] cert = new byte[128]; bb.get(cert); @@ -2107,7 +2142,7 @@ public class SydApi4j implements SydApi { // r.put("certDNs",certs.toArray(new String[0]) ); - r.put("certDNs",certs); + r.put("certDNs", certs); return r; } @@ -2120,9 +2155,7 @@ public class SydApi4j implements SydApi { } /** - * @param certData - * @param organizationId - * @return + * */ @Override public RetWrap importCertAddHT(String certData, byte[] organizationId) { @@ -2140,8 +2173,8 @@ public class SydApi4j implements SydApi { public String importCert(String certData) { Proto8009 proto = new Proto8009(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -2161,7 +2194,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); @@ -2186,12 +2222,13 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dnData, 0, s.length); // 填充数据 - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -2199,7 +2236,6 @@ public class SydApi4j implements SydApi { data.put("certDn", dnData); - // 渲染发送 ByteBuffer bb = proto.rend(packet); @@ -2217,12 +2253,15 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((be[0] ^ be[1]) != 0) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("删除失败=" + e.toString()); + println("删除失败=" + e); return false; } else { return true; @@ -2236,9 +2275,9 @@ public class SydApi4j implements SydApi { Proto8009 proto = new Proto8009(); // 填充数据 - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -2270,7 +2309,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); @@ -2444,7 +2486,7 @@ public class SydApi4j implements SydApi { } StopWatch stopWatch = new StopWatch(); stopWatch.start(); - log.info("socket收发信息异常,准备重试:{}/当前重试:{}次", e.getMessage(),retryCount + 1); + log.info("socket收发信息异常,准备重试:{}/当前重试:{}次", e.getMessage(), retryCount + 1); System.out.println(DateTime.now() + "/socket收发信息异常:" + e.getMessage() + ",准备重试, 当前重试:" + ++retryCount + "次"); boolean currentState = usingBackup.get(); @@ -2491,7 +2533,7 @@ public class SydApi4j implements SydApi { buff = new byte[bb.position()]; System.arraycopy(bb.array(), 0, buff, 0, buff.length); } - if(debug){ + if (debug) { println("\n\n发送数据"); // print("原始数据: \t"); // println(Arrays.toString(buff)); @@ -2500,14 +2542,13 @@ public class SydApi4j implements SydApi { if (null != builder && builder.isUseEBCDMode()) { // 对十个字节之后的进行 EBCD 转码 - buff = CoderUtil.ASCIIToEBCDICFIX(buff, 10); + CoderUtil.ASCIIToEBCDICFIX(buff, 10); print("EBCD 数据: \t"); println(Util.bytes2HexString(buff)); } } - testSocket(); try { @@ -2541,8 +2582,6 @@ public class SydApi4j implements SydApi { /** * 开放此接口 - * - * @param buff */ public void syncSend(byte[] buff) { @@ -2593,7 +2632,7 @@ public class SydApi4j implements SydApi { len += 2; - if(debug){ + if (debug) { print("H\t"); println(Integer.toHexString(high)); print("L\t"); @@ -2627,8 +2666,6 @@ public class SydApi4j implements SydApi { } - - if (null != builder && builder.isUseEBCDMode()) { // print("EBCD 数据: \t"); // println(Util.bytes2HexString(bb.array())); @@ -2636,7 +2673,7 @@ public class SydApi4j implements SydApi { byte[] b = bb.array(); CoderUtil.EBCDICToASCIIFIX(b, 10); } - if(debug){ + if (debug) { // print("EBCD 数据: \t"); // println(Util.bytes2HexString(bb.array())); print("HEX数据: \t"); @@ -2652,15 +2689,13 @@ public class SydApi4j implements SydApi { * 阻塞读取 * 前两个字节是长度 * - * @return * @buffer 如果出错,需要补发的数据。 */ - protected ByteBuffer syncRead(byte[] buffer) { + protected ByteBuffer syncRead(byte[] ignoredBuffer) { // println("\n\n接收数据"); try { // 正常接收 - ByteBuffer bb = read(socket); - return bb; + return read(socket); } catch (SocketTimeoutException e) { throw new SydApiException("接收数据超时", -2); @@ -2764,7 +2799,7 @@ public class SydApi4j implements SydApi { this.retryWhenNetError = retryWhenNetError; this.ip = ip; this.port = port; - this.pwd = pwd; +// this.pwd = pwd; this.connectTimeout = connectTimeout; this.readTimeout = readTimeout; @@ -2785,7 +2820,6 @@ public class SydApi4j implements SydApi { /** * @param e 原始错误 - * @throws SydApiException */ private Socket reConnect(SydApiException e) { while (this.retryWhenNetError > 0) { @@ -2818,22 +2852,20 @@ public class SydApi4j implements SydApi { if (null != socket) { try { socket.getInputStream().close(); - } catch (Exception e1) { - } finally { + } catch (Exception ignored) { } try { socket.getOutputStream().close(); - } catch (Exception e2) { - } finally { + } catch (Exception ignored) { + ignored.printStackTrace(); } try { socket.close(); - } catch (Exception e) { - } finally { + } catch (Exception ignored) { } } @@ -2854,67 +2886,51 @@ public class SydApi4j implements SydApi { private byte[] repackEncrypt(HashMap packet) { +// HashMap data = (HashMap) packet.get("data"); + @SuppressWarnings("unchecked") HashMap data = (HashMap) packet.get("data"); int v = ((byte[]) data.get("msg")).length; final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); - repack("E0", 2048, packet, new PackCallBack() { - - @Override - public void circle(int packSn, HashMap packet, Map ret) { - bbs.put(((PacketSection) ret.get("msg")).getBytes()); - } - - }); + repack("E0", 2048, packet, (packSn, packet1, ret) -> bbs.put(((PacketSection) ret.get("msg")).getBytes())); return Util.toArray(bbs); } private RetWrap repackEncrypt(HashMap packet, Boolean isDe) { - HashMap data = (HashMap) packet.get("data"); - int v = ((byte[]) data.get("msg")).length; +// HashMap data = (HashMap) packet.get("data"); + int v = ((byte[]) packet.get("msg")).length; final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); int packSize = 4064; // 2048/3072/4064 int packSizeDe = packSize + 16; // int fm = (int) data.get("fullMode$option"); - int fm = (Integer) data.get("fullMode$option"); + int fm = (Integer) packet.get("fullMode$option"); if (isDe && 1 == fm && v <= packSizeDe && v > packSize) { //说明传入数据正好是2048,pkcs模式下机密来的,解密时需当做唯一包处理 packSize = packSizeDe; } - RetWrap retWrap = repack("E0", packSize, packet, new PackCallBack() { - @Override - public void circle(int packSn, HashMap packet, Map ret) { - bbs.put(((PacketSection) ret.get("msg")).getBytes()); - - } - }); //return Util.toArray(bbs); - return retWrap; + return repack("E0", packSize, packet, (packSn, packet1, ret) -> bbs.put(((PacketSection) ret.get("msg")).getBytes())); } private byte[] repackHash(HashMap packet) { - HashMap data = (HashMap) packet.get("data"); - int v = ((byte[]) data.get("msg")).length; - final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); +// HashMap data = (HashMap) packet.get("data"); +// int v = ((byte[]) data.get("msg")).length; +// final ByteBuffer bbs = ByteBuffer.allocate(v + 1024); final byte[][] hash = new byte[1][0]; - repack("76", 2048, packet, new PackCallBack() { - - @Override - public void circle(int packNo, HashMap packet, Map ret) { - if (0 == packNo || 3 == packNo) { - hash[0] = ((PacketSection) ret.get("hash")).getBytes(); - } else { - byte[] filterData = ((PacketSection) ret.get("filterData")).getBytes(); - HashMap data = (HashMap) packet.get("data"); - data.put("filterData", filterData); - } + repack("76", 2048, packet, (packNo, packet1, ret) -> { + if (0 == packNo || 3 == packNo) { + hash[0] = ((PacketSection) ret.get("hash")).getBytes(); + } else { + byte[] filterData = ((PacketSection) ret.get("filterData")).getBytes(); + HashMap data = (HashMap) packet1.get("data"); + data.put("filterData", filterData); } }); @@ -2923,11 +2939,11 @@ public class SydApi4j implements SydApi { /** * 分包发送接收 - * - * @param packSize */ - private RetWrap repack(String cmd, int packSize, HashMap packet, PackCallBack cb) { + private RetWrap repack(String ignoredCmd, int packSize, HashMap packet, PackCallBack cb) { +// HashMap data = (HashMap) packet.get("data"); + @SuppressWarnings("unchecked") HashMap data = (HashMap) packet.get("data"); int v = ((byte[]) data.get("msg")).length; @@ -2936,9 +2952,9 @@ public class SydApi4j implements SydApi { //>2k,即进行分块,首尾包进行解密-> 报错 //如果是PKCS模式,packSize应为2064,才能当做唯一包进行解密 - int fm = (Integer) data.get("fullMode$option"); - int loop = 0; - int tailLen = 0; +// int fm = (Integer) data.get("fullMode$option"); + int loop; + int tailLen; //解密时,PKCS,2048) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -3142,20 +3156,20 @@ public class SydApi4j implements SydApi { // msg_all 类型改 List, 当 唯一包、尾包时还原 byte[] 类型 if (retWrap.containsKey("msg_all")) { BytesList msg_all = (BytesList) retWrap.get("msg_all"); - msg_all.append( res_msg ); + msg_all.append(res_msg); } else { - retWrap.put("msg_all", new BytesList(res_msg) ); + retWrap.put("msg_all", new BytesList(res_msg)); } if (0 == i) { if (1 == loop) { // 唯一包 - retWrap.put("msg_all", ((BytesList)retWrap.get("msg_all")).toBytes() ); + retWrap.put("msg_all", ((BytesList) retWrap.get("msg_all")).toBytes()); } } else { if (i == (loop - 1)) { // 尾包 - retWrap.put("msg_all", ((BytesList)retWrap.get("msg_all")).toBytes() ); + retWrap.put("msg_all", ((BytesList) retWrap.get("msg_all")).toBytes()); } } cb.circle(packNo, packet, ret); @@ -3174,10 +3188,10 @@ public class SydApi4j implements SydApi { } int packSize = 8000; - int readSize = 8000; + int readSize; // 获取文件长度 - int lenIndata = 0; + int lenIndata; File file = new File(inputPath); if (file.exists() && file.isFile()) { lenIndata = (int) file.length(); @@ -3209,8 +3223,14 @@ public class SydApi4j implements SydApi { File outFile = new File(outputPath); //清空文件 try { +// if (!outFile.exists()) { +// outFile.createNewFile(); +// } if (!outFile.exists()) { - outFile.createNewFile(); + boolean created = outFile.createNewFile(); + if (!created) { + throw new IOException("创建文件失败: " + outputPath); + } } FileWriter fileWriter = new FileWriter(outFile); fileWriter.write(""); @@ -3225,9 +3245,9 @@ public class SydApi4j implements SydApi { for (int i = 0; i < loop; i++) { PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); switch (nAlg) { case 0: @@ -3272,8 +3292,14 @@ public class SydApi4j implements SydApi { // 中间包 data.put("packSn$option", 2); readSize = packSize; +// byte[] msg = new byte[readSize]; +// fileInputStream.read(msg); byte[] msg = new byte[readSize]; - fileInputStream.read(msg); + int bytesRead = fileInputStream.read(msg); + if (bytesRead != readSize) { + throw new IOException("读取数据不完整,期望读取: " + readSize + " 字节,实际读取: " + bytesRead + " 字节"); + } + println("\n\t中间包"); data.put("msg", msg); } @@ -3351,8 +3377,8 @@ public class SydApi4j implements SydApi { public RetWrap SYMEnDeData(Boolean isEn, int nAlg, String seesionKey, int roundmod, int fullMode, byte[] pInData, byte[] iv, int block) { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); // 确认算法 if (isEn) { @@ -3403,11 +3429,10 @@ public class SydApi4j implements SydApi { // Map map = repackEncrypt(packet); // Map map = repackEncrypt(packet,!isEn); - RetWrap retWrap = repackEncrypt(packet, !isEn); // System.out.println(retWrap); - return retWrap; + return repackEncrypt(packet, !isEn); } /** @@ -3417,7 +3442,6 @@ public class SydApi4j implements SydApi { * @param userId 生成密钥时的机构号。 * @param index 密钥索引 * @param data 待加密的数据 - * @return */ public byte[] SYMSM4EncDataByIndex(int type, String userId, String index, byte[] data) { @@ -3440,8 +3464,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); switch (nAlg) { case 0: @@ -3555,7 +3579,7 @@ public class SydApi4j implements SydApi { byte[] code = new byte[2]; bb.get(code); - int retCode = Integer.valueOf(new String(code)); + int retCode = Integer.parseInt(new String(code)); if (0 != retCode) { throw new SydApiException(retCode); } @@ -3571,7 +3595,7 @@ public class SydApi4j implements SydApi { lens[i] = Integer.valueOf(new String(iLenArray), 16); } - List ret = new ArrayList(); + List ret = new ArrayList<>(); // 获取 N 个长度的数据 for (int l : lens) { byte[] d = new byte[l]; @@ -3591,8 +3615,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); switch (nAlg) { case 0: @@ -3629,8 +3653,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); switch (nAlg) { case 0: @@ -3671,7 +3695,6 @@ public class SydApi4j implements SydApi { * @param userId 生成密钥时的机构号 * @param index 密钥索引 * @param pINdata 要解密的报文密文。长度必须是8 或16 的倍数 - * @return */ public byte[] SYMSM4DecDataByIndex(int type, String userId, String index, byte[] pINdata) { @@ -3697,9 +3720,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); switch (nAlg) { case 0: @@ -3749,9 +3772,9 @@ public class SydApi4j implements SydApi { Proto6A proto = new Proto6A(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("alg$option", nAlg - 0x30); if (pFlag == 1) { data.put("pFlag", 'P'); @@ -3783,7 +3806,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -3817,9 +3842,9 @@ public class SydApi4j implements SydApi { @Override - public RetWrap generateDEByDnICBC(String certSerial,String IV, byte[] oData,int roundmod ) { + public RetWrap generateDEByDnICBC(String certSerial, String IV, byte[] oData, int roundmod) { - // roundmod 轮模式,密钥加密模式:1=ECB,2=CBC + // roundmod 轮模式,密钥加密模式:1=ECB,2=CBC certSerial = verifyDn(certSerial); @@ -3861,20 +3886,20 @@ public class SydApi4j implements SydApi { // 加密数据 byte[] iv; - if(IV == null || IV.isEmpty()){ + if (IV == null || IV.isEmpty()) { String random = GenerateRandom(16); - System.out.println("random:"+random); + System.out.println("random:" + random); iv = random.getBytes(); - // Arrays.fill(iv, (byte) 0x30); - }else{ + // Arrays.fill(iv, (byte) 0x30); + } else { // if(32 != IV.getBytes().length){ // throw new SydApiException("IV长度需为32位", 0x81); // } iv = IV.getBytes(); } - /** - * E0 指令复合接口 - * @param isEn 指定加解密,true为加密,false为解密 + /* + E0 指令复合接口 + @param isEn 指定加解密,true为加密,false为解密 * @param nAlg 密钥模式,当isEn为true时,默认为SM4加密,0为DES加密,1为AES256 加密。当isEn为false时,默认为SM4解密,0为DES解密,1为AES256解密 * @param seesionKey 密钥 * @param roundmod 轮模式,密钥加密模式:1=ECB,2=CBC,3=CFB,4=OFB @@ -3889,14 +3914,14 @@ public class SydApi4j implements SydApi { RetWrap retWrap = SYMEnDeData(true, 1, sessionKeyByLMK, roundmod, 1, oData, iv, 0); byte[] enData = (byte[]) retWrap.get("msg_all"); - // System.out.println("加密结果:"+Util.bytes2HexString(enData)); + // System.out.println("加密结果:"+Util.bytes2HexString(enData)); // 组织格式 ContentInfo cms = genCMSEnvelopedData(Util.hexString2Bytes(ciperKey), enData, x509.getSerialNumber().toString(16), - dnToMap(issuser.getName()),Util.hexString2Bytes(new String(iv)) + dnToMap(issuser.getName()), Util.hexString2Bytes(new String(iv)) ); - // System.out.println("信封结果:"+Util.bytes2HexString(cms.getEncoded(DL))); + // System.out.println("信封结果:"+Util.bytes2HexString(cms.getEncoded(DL))); ret.put(SydApi.RET_CIPHER_KEY, Util.encodeBase64(cms.getEncoded(DL))); @@ -3915,28 +3940,27 @@ public class SydApi4j implements SydApi { if (oData.length <= 4 * 1024) { return generateDEByDnHsmRSA(alg, certSerial, oData); } else { - return generateDEByDnSoftRSA(alg, certSerial, oData); + return generateDEByDnSoftRSA(certSerial, oData); } } - public RetWrap generateDEByDnAll(int alg,String certSerial, byte[] oData) { - String cert = null; + public RetWrap generateDEByDnAll(int alg, String certSerial, byte[] oData) { + String cert; try { cert = getCertByDn(certSerial); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } boolean sm2Cert = CertUtil.isSM2Cert(cert); - if(sm2Cert){ - return generateDEByDn(certSerial,oData); - }else{ - return generateDEByDnRSA(alg,certSerial,oData); + if (sm2Cert) { + return generateDEByDn(certSerial, oData); + } else { + return generateDEByDnRSA(alg, certSerial, oData); } } - public RetWrap generateDEByDnHsm(String certSerial, byte[] oData) { certSerial = verifyDn(certSerial); @@ -3968,10 +3992,10 @@ public class SydApi4j implements SydApi { Proto6A proto = new Proto6A(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("alg$option", alg); data.put("certificateModel$option", model); @@ -4010,7 +4034,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4034,9 +4060,9 @@ public class SydApi4j implements SydApi { Proto6B proto = new Proto6B(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", certSerial); data.put("key", pCipherKey); @@ -4059,7 +4085,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4083,7 +4111,7 @@ public class SydApi4j implements SydApi { public RetWrap decryptDEByDNHsm(String certSerial, String pCipherKey) { - // certSerial = verifyDn(certSerial); + // certSerial = verifyDn(certSerial); byte[] dnBytes = null; try { @@ -4110,12 +4138,9 @@ public class SydApi4j implements SydApi { /** * 通过 DN 查找证书 - * - * @param dn - * @return */ public String getCertByDn(String dn) throws UnsupportedEncodingException { - byte[] dnBytes = null; + byte[] dnBytes; try { String dnsort = verifyDn(dn); dnBytes = dnsort.getBytes("gbk"); @@ -4127,8 +4152,8 @@ public class SydApi4j implements SydApi { } - public String getCertByDn(String dn,int type) throws UnsupportedEncodingException { - byte[] dnBytes = null; + public String getCertByDn(String dn, int type) throws UnsupportedEncodingException { + byte[] dnBytes; try { String dnsort = verifyDn(dn); dnBytes = dnsort.getBytes("gbk"); @@ -4136,7 +4161,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); throw e; } - return getCertByDn(dnBytes,type); + return getCertByDn(dnBytes, type); } public String getCertByDn(byte[] dnBytes) { @@ -4144,7 +4169,7 @@ public class SydApi4j implements SydApi { // head PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); +// HashMap packet = new HashMap<>(); // data byte[] data = new byte[256]; @@ -4195,12 +4220,12 @@ public class SydApi4j implements SydApi { return new String(cert); } - public String getCertByDn(byte[] dnBytes,int type) { + public String getCertByDn(byte[] dnBytes, int type) { // 8010 指令,没有指令配置文件 // head PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); +// HashMap packet = new HashMap<>(); // data byte[] data = new byte[256]; @@ -4222,10 +4247,10 @@ public class SydApi4j implements SydApi { // 256 Data bb.put(data); - if(type ==0 ){ + if (type == 0) { // 签名 bb.put((byte) 0x30); - }else{ + } else { // 加密 bb.put((byte) 0x31); } @@ -4328,7 +4353,7 @@ public class SydApi4j implements SydApi { } } - public RetWrap generateDEByDnSoftRSA(int alg, String certSerial, byte[] oData) { + public RetWrap generateDEByDnSoftRSA(String certSerial, byte[] oData) { certSerial = verifyDn(certSerial); @@ -4393,7 +4418,7 @@ public class SydApi4j implements SydApi { } private static Map dnToMap(String dn) { - Map map = new HashMap(); + Map map = new HashMap<>(); String[] kvs = dn.split(","); for (String kv : kvs) { String[] ps = kv.split("="); @@ -4450,12 +4475,12 @@ public class SydApi4j implements SydApi { ASN1Set setO = (ASN1Set) seq4.getObjectAt(1).toASN1Primitive(); ASN1Sequence seqO = (ASN1Sequence) setO.getObjectAt(0).toASN1Primitive(); DERUTF8String o = (DERUTF8String) seqO.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes("UTF-8")); + BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes(StandardCharsets.UTF_8)); ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); DERUTF8String Cn = (DERUTF8String) seqCn.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes("UTF-8")); + BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes(StandardCharsets.UTF_8)); // 密文段 ASN1Sequence seq5 = (ASN1Sequence) seq.getObjectAt(2).toASN1Primitive(); @@ -4466,7 +4491,7 @@ public class SydApi4j implements SydApi { } - public static ContentInfo genCMSEnvelopedData(byte[] sessionKeyDer, byte[] enData, String sn, Map issuer,byte[] iv) throws UnsupportedEncodingException { + public static ContentInfo genCMSEnvelopedData(byte[] sessionKeyDer, byte[] enData, String sn, Map issuer, byte[] iv) throws UnsupportedEncodingException { //String tmpl = "3082014D06092A864886F70D010703A082013E3082013A0201003181F73081F40201003065305C310B300906035504061302434E3130302E060355040A0C274368696E612046696E616E6369616C2043657274696669636174696F6E20417574686F72697479311B301906035504030C1243464341205445535420534D32204F43413102051004282452300B06092A811CCF5501822D03047B307902203C3AF535307DA548DA02C0AD171D2BC24D22472B42E5E175FCAF2C5CD30B5443022100ED53E7594F35FFACEB57F05898123F78E5B2D045CC47D894CD2E7BD34D66DA1D042014BD71F8CDA9729093A7CC391E2009C5D5F007586C2EBFB55BC175A6E8BDA33E04104AB82510BFDDD4FEE79C3F107D6F7F94303B060A2A811CCF550601040201301B06072A811CCF55016804100000000000000000000000000000000080109FD3D4338DB19E672CC162AF5B9B03E9"; String tmpl = "3082014E060A2A811CCF550601040203A082013E3082013A0201003181F73081F40201003065305C310B300906035504061302434E3130302E060355040A0C274368696E612046696E616E6369616C2043657274696669636174696F6E20417574686F72697479311B301906035504030C1243464341205445535420534D32204F43413102051004282452300B06092A811CCF5501822D03047B307902203C3AF535307DA548DA02C0AD171D2BC24D22472B42E5E175FCAF2C5CD30B5443022100ED53E7594F35FFACEB57F05898123F78E5B2D045CC47D894CD2E7BD34D66DA1D042014BD71F8CDA9729093A7CC391E2009C5D5F007586C2EBFB55BC175A6E8BDA33E04104AB82510BFDDD4FEE79C3F107D6F7F94303B060A2A811CCF550601040201301B06072A811CCF55016804100000000000000000000000000000000080109FD3D4338DB19E672CC162AF5B9B03E9"; ContentInfo info = SydCmsUtil.getContentInfo(Util.hexString2Bytes(tmpl)); @@ -4489,12 +4514,12 @@ public class SydApi4j implements SydApi { ASN1Set setO = (ASN1Set) seq4.getObjectAt(1).toASN1Primitive(); ASN1Sequence seqO = (ASN1Sequence) setO.getObjectAt(0).toASN1Primitive(); DERUTF8String o = (DERUTF8String) seqO.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes("UTF-8")); + BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes(StandardCharsets.UTF_8)); ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); DERUTF8String Cn = (DERUTF8String) seqCn.getObjectAt(1).toASN1Primitive(); - BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes("UTF-8")); + BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes(StandardCharsets.UTF_8)); // 密文段 ASN1Sequence seq5 = (ASN1Sequence) seq.getObjectAt(2).toASN1Primitive(); @@ -4503,7 +4528,7 @@ public class SydApi4j implements SydApi { BeanUtil.setFieldValue(data, "string", enData); //iv段 - ASN1Sequence seq6 = (ASN1Sequence)seq5.getObjectAt(1).toASN1Primitive(); + ASN1Sequence seq6 = (ASN1Sequence) seq5.getObjectAt(1).toASN1Primitive(); DEROctetString ivtag = (DEROctetString) seq6.getObjectAt(1).toASN1Primitive(); BeanUtil.setFieldValue(ivtag, "string", iv); @@ -4529,14 +4554,14 @@ public class SydApi4j implements SydApi { BeanUtil.setFieldValue(isn, "bytes", Util.hexString2Bytes(sn)); //签发者名称 - ASN1Sequence seq4 = (ASN1Sequence) seq3.getObjectAt(0).toASN1Primitive(); +// ASN1Sequence seq4 = (ASN1Sequence) seq3.getObjectAt(0).toASN1Primitive(); // ASN1Set setO = (ASN1Set) seq4.getObjectAt(1).toASN1Primitive(); // ASN1Sequence seqO = (ASN1Sequence) setO.getObjectAt(0).toASN1Primitive(); // DERUTF8String o = (DERUTF8String) seqO.getObjectAt(1).toASN1Primitive(); // BeanUtil.setFieldValue(o, "string", issuer.get("O").getBytes("UTF-8") ); - ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); - ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); +// ASN1Set setCN = (ASN1Set) seq4.getObjectAt(2).toASN1Primitive(); +// ASN1Sequence seqCn = (ASN1Sequence) setCN.getObjectAt(0).toASN1Primitive(); // DERUTF8String Cn = (DERUTF8String) seqCn.getObjectAt(1).toASN1Primitive(); // BeanUtil.setFieldValue(Cn, "string", issuer.get("CN").getBytes("UTF-8") ); @@ -4557,7 +4582,7 @@ public class SydApi4j implements SydApi { } } - public RetWrap decryptDEByDNICBC(String certSerial, String IV,String pCipherKey,int roundmod) { + public RetWrap decryptDEByDNICBC(String certSerial, String IV, String pCipherKey, int roundmod) { certSerial = verifyDn(certSerial); @@ -4568,7 +4593,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - RetWrap ret = null; + RetWrap ret; try { ret = deCMSEnvelopedData(Util.decodeBase64(pCipherKey)); @@ -4581,8 +4606,8 @@ public class SydApi4j implements SydApi { // 解密原文 byte[] iv = Util.bytes2HexString((byte[]) ret.get("iv")).getBytes(); //new byte[32]; - System.out.println("实际iv值hex:"+Util.bytes2HexString((byte[]) ret.get("iv"))); - if(null != IV && !IV.isEmpty()){ + System.out.println("实际iv值hex:" + Util.bytes2HexString((byte[]) ret.get("iv"))); + if (null != IV && !IV.isEmpty()) { iv = IV.getBytes(); } // 解密会话密钥 @@ -4607,18 +4632,18 @@ public class SydApi4j implements SydApi { } } - public RetWrap decryptDEByDNAll(int alg,String certSerial, byte[] pCipherKey) { - String cert = null; + public RetWrap decryptDEByDNAll(int alg, String certSerial, byte[] pCipherKey) { + String cert; try { cert = getCertByDn(certSerial); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } boolean sm2Cert = CertUtil.isSM2Cert(cert); - if(sm2Cert){ - return decryptDEByDN(certSerial,pCipherKey); - }else{ - return decryptDEByDNRSA(alg,certSerial,pCipherKey); + if (sm2Cert) { + return decryptDEByDN(certSerial, pCipherKey); + } else { + return decryptDEByDNRSA(alg, certSerial, pCipherKey); } } @@ -4633,7 +4658,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - RetWrap ret = null; + RetWrap ret; try { ret = deCMSEnvelopedData(Util.decodeBase64(pCipherKey)); @@ -4672,7 +4697,7 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } - RetWrap ret = null; + RetWrap ret; try { ret = deCMSEnvelopedData(Util.decodeBase64(pCipherKey)); @@ -4712,10 +4737,10 @@ public class SydApi4j implements SydApi { Proto6BXF proto = new Proto6BXF(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); if (pFlag) { data.put("pFlag", "P"); } @@ -4749,7 +4774,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4783,10 +4811,10 @@ public class SydApi4j implements SydApi { Proto6BXF proto = new Proto6BXF(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("pFlag", "R"); if (!pFlag) { data.put("pFlag", "Q"); @@ -4819,7 +4847,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4853,9 +4884,9 @@ public class SydApi4j implements SydApi { Proto6BRSA proto = new Proto6BRSA(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("pFlag", "R"); if (!pFlag) { data.put("pFlag", "Q"); @@ -4887,7 +4918,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4922,9 +4956,9 @@ public class SydApi4j implements SydApi { //填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("nRandomSize", nRandomSize); packet.put("data", data); @@ -4942,7 +4976,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -4972,9 +5009,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -4996,7 +5033,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5024,9 +5064,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5048,7 +5088,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5072,9 +5115,9 @@ public class SydApi4j implements SydApi { Proto6A proto = new Proto6A(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); try { data.put("alg$option", 3); //data.put("pFlag", "P"); @@ -5110,7 +5153,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5136,9 +5182,9 @@ public class SydApi4j implements SydApi { Proto6B proto = new Proto6B(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); try { data.put("pFlag", "P"); data.put("pSignFlag", "P"); @@ -5170,15 +5216,17 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } RetWrap rw = new RetWrap(); rw.put("oData", ((PacketSection) ret.get("oData")).getBytes()); - byte[] odata = (byte[]) rw.get("oData"); - return odata; + return (byte[]) rw.get("oData"); // int orgCodeLen = Util.a2i(((PacketSection) ret.get("orgCodeLen")).getBytes(), 10); // if (orgCodeLen != 0) { @@ -5213,9 +5261,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5237,7 +5285,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5265,9 +5316,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5289,7 +5340,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5317,9 +5371,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5340,7 +5394,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5357,9 +5414,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", 0); data.put("certificateSn", pCertSerial); data.put("inData", pOrgData); @@ -5380,7 +5437,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5398,9 +5457,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("certificateModel$option", nCertType >= 0x30 ? nCertType - 0x30 : nCertType); data.put("certificateDn", certDN); packet.put("data", data); @@ -5419,7 +5478,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5433,8 +5495,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("E2"); ProtoE2 proto = new ProtoE2(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5451,7 +5513,7 @@ public class SydApi4j implements SydApi { //ProtocolParser parser = pset.getProtocolResParse(); - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); @@ -5461,7 +5523,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5481,8 +5546,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap SM3Hash(int packSn, String userId, byte[] publicKey, byte[] msg, byte[] filterData) { Proto76 proto = new Proto76(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -5498,13 +5563,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5527,8 +5594,8 @@ public class SydApi4j implements SydApi { System.out.println(getClass().getClassLoader().hashCode()); ProtoE2 proto = new ProtoE2(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); int keyLength = -1; if (keyType == 0) { @@ -5554,13 +5621,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5579,15 +5648,14 @@ public class SydApi4j implements SydApi { public boolean RSAVerify(int HashMark, int nOrgDataType, byte[] publicKey, byte[] orgData, byte[] base64Sign) { Proto7C proto = new Proto7C(); - byte[] sign = base64Sign; - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); data.put("signatureDataType$option", nOrgDataType); data.put("hashMark$option", HashMark); - data.put("signature", sign); + data.put("signature", base64Sign); data.put("hashData", orgData); data.put("infoData", orgData); data.put("publicKey", publicKey); @@ -5603,7 +5671,9 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if ("02".equals(error.getString())) { return false; @@ -5619,8 +5689,8 @@ public class SydApi4j implements SydApi { @Override public byte[] RSASignC(String privateKeyIndex, int HashMark, int nOrgDataType, byte[] orgData) { Proto7B proto = new Proto7B(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5664,8 +5734,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap genSymmetricKey(String keyType, char solution) { ProtoA0 proto = new ProtoA0(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -5680,13 +5750,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5708,8 +5780,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genSymKey(int mode, String keyType, char solution, String zmk) { ProtoA0 proto = new ProtoA0(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -5727,13 +5799,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5766,8 +5840,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("QQ"); ProtoQQ proto = new ProtoQQ(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5787,7 +5861,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5811,8 +5888,8 @@ public class SydApi4j implements SydApi { public String SM2GetPublicKeyC(String privateKeyTag) { ProtoQQ proto = new ProtoQQ(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5833,7 +5910,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5851,8 +5930,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("QQ"); ProtoQQ proto = new ProtoQQ(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5873,7 +5952,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5903,8 +5984,8 @@ public class SydApi4j implements SydApi { ProtoQP proto = new ProtoQP(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5925,7 +6006,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5944,8 +6027,7 @@ public class SydApi4j implements SydApi { /** * 通过私钥获取公钥 * - * @param userId 密钥对应的索引号 - * @param nKeyType + * @param userId 密钥对应的索引号 * @return 公钥 */ @Override @@ -5953,8 +6035,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("QQ"); ProtoQX proto = new ProtoQX(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -5981,7 +6063,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -5999,8 +6083,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender(new byte[]{(byte) 0x80, 0x16}); Proto8016 proto = new Proto8016(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6042,17 +6126,16 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); }*/ - byte[] pCertInfo = s; //byte[] pCertInfo = ((PacketSection) ret.get("certInfo")).getBytes(); //Util.bytes2HexString(pCertInfo); X509 cinfo = new X509(); - cinfo.setIssuer(Util.toCleanString(pCertInfo, 0, 257)); - cinfo.setSerialNumber(Util.toCleanString(pCertInfo, 257, 65)); - cinfo.setSubject(Util.toCleanString(pCertInfo, 257 + 65, 257)); - cinfo.setNotBefore(Util.toCleanString(pCertInfo, 257 + 65 + 257, 15)); - cinfo.setNotAfter(Util.toCleanString(pCertInfo, 257 + 65 + 257 + 15, 15)); - cinfo.setPubKey(pCertInfo, 257 + 65 + 257 + 15 + 15, pCertInfo.length - (257 + 65 + 257 + 15 + 15)); + cinfo.setIssuer(Util.toCleanString(s, 0, 257)); + cinfo.setSerialNumber(Util.toCleanString(s, 257, 65)); + cinfo.setSubject(Util.toCleanString(s, 257 + 65, 257)); + cinfo.setNotBefore(Util.toCleanString(s, 257 + 65 + 257, 15)); + cinfo.setNotAfter(Util.toCleanString(s, 257 + 65 + 257 + 15, 15)); + cinfo.setPubKey(s, 257 + 65 + 257 + 15 + 15, s.length - (257 + 65 + 257 + 15 + 15)); return cinfo; } @@ -6062,7 +6145,6 @@ public class SydApi4j implements SydApi { * * @param sn sn * @param nCertType 证书类型,0:签名证书, 1:加密证书 - * @return */ @Override public String GetX509CertFileBySN(String sn, int nCertType) { @@ -6081,13 +6163,12 @@ public class SydApi4j implements SydApi { * @param flag 0: SN,1: 机构号 * @param userId 根据flag确定传入的数据 * @param nCertType 证书类型,0:签名证书, 1:加密证书 - * @return */ private String GetX509CertFileC(int flag, String userId, int nCertType) { Proto8017 proto = new Proto8017(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6125,7 +6206,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); @@ -6159,8 +6243,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -6217,8 +6301,8 @@ public class SydApi4j implements SydApi { byte[] sign = SYMUtil.base64Sign2DerSign(base64Sign); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6230,7 +6314,7 @@ public class SydApi4j implements SydApi { // 渲染发送 ByteBuffer bb = proto.rend(packet); - long b = System.currentTimeMillis(); +// long b = System.currentTimeMillis(); synchronized (this) { // 响应解析 @@ -6245,7 +6329,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -6268,8 +6354,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6333,8 +6419,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6357,7 +6443,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -6398,8 +6486,8 @@ public class SydApi4j implements SydApi { Proto74 proto = new Proto74(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6448,8 +6536,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6472,13 +6560,11 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); - if (!"00".equals(error.getString())) { - - return false; - } - return true; + return "00".equals(error.getString()); } @@ -6496,8 +6582,8 @@ public class SydApi4j implements SydApi { sign = SYMUtil.base64Sign2DerSign(Util.encodeBase64(sign)); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6510,7 +6596,7 @@ public class SydApi4j implements SydApi { // 渲染发送 ByteBuffer bb = proto.rend(packet); - long b = System.currentTimeMillis(); +// long b = System.currentTimeMillis(); synchronized (this) { // 响应解析 @@ -6525,7 +6611,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -6565,7 +6653,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Packet p = proto.parse(bb); +// Packet p = proto.parse(bb); DTO79.Res res = new DTO79.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6605,7 +6693,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Packet p = proto.parse(bb); +// Packet p = proto.parse(bb); DTO7A.Res res = new DTO7A.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6639,7 +6727,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Packet p = proto.parse(bb); +// Packet p = proto.parse(bb); DTO77.Res res = new DTO77.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6696,7 +6784,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Packet p = proto.parse(bb); +// Packet p = proto.parse(bb); DTO78.Res res = new DTO78.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6731,7 +6819,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Packet p = proto.parse(bb); +// Packet p = proto.parse(bb); DTO78.Res res = new DTO78.Res(); res = res.fullFromSoup(proto.parse(bb).resSoup()); @@ -6782,7 +6870,7 @@ public class SydApi4j implements SydApi { * * @param sk HEX 私钥值 * @param enData base64 值 - * @return + * @return 解密后的明文数据 */ @Override public byte[] RSADecrypt(String sk, String enData) { @@ -6792,14 +6880,13 @@ public class SydApi4j implements SydApi { } - @Override public String SM2EncryptOrder(byte[] publicKey, byte[] pOrgData) { Proto77 proto = new Proto77(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -6820,7 +6907,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -6830,7 +6919,7 @@ public class SydApi4j implements SydApi { } - public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData){ + public byte[] SYD_SM2Encrypt(byte[] publicKey, byte[] pOrgData) { ParamChecker.checkNotNull(publicKey, "publicKey"); ParamChecker.checkNotNull(pOrgData, "pOrgData"); @@ -6889,30 +6978,30 @@ public class SydApi4j implements SydApi { // 最大缓存 64K // 根据输入数据快速估算缓存大小 int buffLen = 4096; - if( 1 == dataType ) { - buffLen = pOrgData.limit() > 4096 ? 64*1024 : 5*1024; + if (1 == dataType) { + buffLen = pOrgData.limit() > 4096 ? 64 * 1024 : 5 * 1024; } - ByteBuffer bb = ByteBuffer.allocate(buffLen ); + ByteBuffer bb = ByteBuffer.allocate(buffLen); PacketSN sn = PacketSN.gen(); bb.put(new byte[2]); bb.put(sn.getSn().array()); bb.put("7F".getBytes()); bb.put((byte) dataType); // 数据包标志 - if ( 2 == dataType ) { // 公钥 + if (2 == dataType) { // 公钥 bb.put(ByteUtil.shortToBytes((short) publicKey.length)); bb.put(publicKey); } - if ( 1 == dataType ) { // 数据 + if (1 == dataType) { // 数据 bb.put(ByteUtil.shortToBytes((short) pOrgData.limit())); bb.put(pOrgData); - bb.put(ByteUtil.shortToBytes((short)idx)); + bb.put(ByteUtil.shortToBytes((short) idx)); } - if ( 3 == dataType) { // 仅数据长度 - bb.put( ByteUtil.shortToBytes((short) (63*1024)) ); - bb.put( ByteUtil.shortToBytes((short)idx)); + if (3 == dataType) { // 仅数据长度 + bb.put(ByteUtil.shortToBytes((short) (63 * 1024))); + bb.put(ByteUtil.shortToBytes((short) idx)); } @@ -6933,13 +7022,13 @@ public class SydApi4j implements SydApi { byte[] code = new byte[2]; bb.get(code); - int retCode = Integer.valueOf(new String(code)); + int retCode = Integer.parseInt(new String(code)); if (0 != retCode) { throw new SydApiException(retCode); } // 只有类型是 3 时有数据结果 - if ( 3 == dataType ){ + if (3 == dataType) { int isLast = bb.get(); byte[] dataLenArr = new byte[2]; bb.get(dataLenArr); @@ -6954,11 +7043,10 @@ public class SydApi4j implements SydApi { } - - /** * SM2 解密(支持大包分段) - * @param privateKey 私钥字节数组 + * + * @param privateKey 私钥字节数组 * @param pCipherData 密文数据 * @return 解密后的明文 */ @@ -7013,10 +7101,11 @@ public class SydApi4j implements SydApi { /** * 底层解密通信(与加密结构对称,魔法数改为 "7G") - * @param dataType 1-传入密文块,2-传入私钥,3-取明文块 - * @param privateKey 私钥(dataType=2时使用) + * + * @param dataType 1-传入密文块,2-传入私钥,3-取明文块 + * @param privateKey 私钥(dataType=2时使用) * @param pCipherData 密文块(dataType=1时使用) - * @param idx 偏移量索引 + * @param idx 偏移量索引 * @return MutiReturn7 包含返回码、isLast标志及数据片 */ private MutiReturn7 SYD_SM2Decrypt(int dataType, byte[] privateKey, ByteBuffer pCipherData, int idx) { @@ -7087,8 +7176,8 @@ public class SydApi4j implements SydApi { Proto77 proto = new Proto77(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7109,7 +7198,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7123,8 +7215,8 @@ public class SydApi4j implements SydApi { Proto77 proto = new Proto77(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7145,7 +7237,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7162,8 +7257,8 @@ public class SydApi4j implements SydApi { byte[] pOrgData = SYMUtil.base64EnData2DerEnData(enData); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7185,7 +7280,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7199,8 +7296,8 @@ public class SydApi4j implements SydApi { Proto78 proto = new Proto78(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7222,7 +7319,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7240,8 +7340,8 @@ public class SydApi4j implements SydApi { byte[] pOrgData = SYMUtil.base64EnData2DerEnData(enData); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7262,7 +7362,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7288,8 +7391,8 @@ public class SydApi4j implements SydApi { byte[] pOrgData = SYMUtil.base64EnData2DerEnDataC1C2C3(enData); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -7310,7 +7413,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7331,7 +7436,7 @@ public class SydApi4j implements SydApi { * @param cdn 证书DN * @param userId 用户ID * @param msg 原始数据,二进制的消息块 - * @return + * @return SM3哈希值 */ private byte[] SM3Hash(String publicKey, String csn, String cdn, String userId, byte[] msg) { @@ -7522,10 +7627,7 @@ public class SydApi4j implements SydApi { // 最后一块正好为空的情况 - boolean hard = false; - if (msg.length == 0 || 3 == block) { - hard = true; - } + boolean hard = msg.length == 0 || 3 == block; byte packtype; int i = 0; @@ -7677,9 +7779,9 @@ public class SydApi4j implements SydApi { Proto72 proto = new Proto72(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("publicKey", Util.hexString2Bytes(publicKey)); data.put("lmk", "X"); packet.put("data", data); @@ -7701,7 +7803,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7727,9 +7831,9 @@ public class SydApi4j implements SydApi { Proto72 proto = new Proto72(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("pFlag", "R"); data.put("publicKey", Util.hexString2Bytes(publicKey)); data.put("lmk", "L"); @@ -7752,7 +7856,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -7778,8 +7884,7 @@ public class SydApi4j implements SydApi { */ @Override public RetWrap genDigitalEnvelope(String publicKey) { - RetWrap retWrap = SM2GenSessionKey(publicKey); - return retWrap; + return SM2GenSessionKey(publicKey); } /** @@ -7802,12 +7907,11 @@ public class SydApi4j implements SydApi { * @param inPutDataStr 报文明文 * @param msgId 设备认证内容 * @return 格式:sessionByPk(base64)+\r\n+头(base64)+\r\n+密文(base64) - * @throws UnsupportedEncodingException */ @Override public String genDigitalEnvelopeWithDM(String keyValue, String inPutDataStr, String msgId) throws UnsupportedEncodingException { String separator = "\r\n"; //分隔符 - byte[] inPutData = null; + byte[] inPutData; if (null != builder) { inPutData = inPutDataStr.getBytes(builder.getEncode()); } else { @@ -7843,7 +7947,7 @@ public class SydApi4j implements SydApi { @Override public String genDigitalEnvelopeWithData(String keyValue, String inPutDataStr) throws UnsupportedEncodingException { String separator = "\r\n"; //分隔符 - byte[] inPutData = null; + byte[] inPutData; if (null != builder) { inPutData = inPutDataStr.getBytes(builder.getEncode()); } else { @@ -7864,11 +7968,9 @@ public class SydApi4j implements SydApi { RetWrap retWrap1 = SYMEnDeData(true, 1, sessionByLMK, 1, 1, inPutData, null, 0); byte[] ret = (byte[]) retWrap1.get("msg_all"); - StringBuilder sb = new StringBuilder(); - sb.append(Util.encodeBase64(Util.hexString2Bytes(sessionByPk_HEX))); - sb.append(separator); - sb.append(Util.encodeBase64(ret)); - return sb.toString(); + return Util.encodeBase64(Util.hexString2Bytes(sessionByPk_HEX)) + + separator + + Util.encodeBase64(ret); } @@ -7897,7 +7999,7 @@ public class SydApi4j implements SydApi { if (null != builder) { return new String((byte[]) retWrap.get("msg_all"), builder.getEncode()); } - return new String((byte[]) retWrap.get("msg_all"), "UTF-8"); + return new String((byte[]) retWrap.get("msg_all"), StandardCharsets.UTF_8); } @Override @@ -7905,8 +8007,6 @@ public class SydApi4j implements SydApi { return "X12345678123456781234567812345678"; } - ; - @Override public RetWrap SM2ConfirmSessionKeyC(String userId, String cipherKey) { @@ -7920,9 +8020,9 @@ public class SydApi4j implements SydApi { Proto73 proto = new Proto73(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); //传全9的情况,私钥需要自己传 //data.put("privateKeyTag", "999999999999999999999999999"); @@ -7950,7 +8050,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -8105,14 +8207,13 @@ public class SydApi4j implements SydApi { * @param msg 消息数据 * @param iv iv * @param block 消息块号 - * @return */ // @Override public RetWrap SM4EncDecDataCBC(Boolean isEn, int keyType, String key, int inType, int outType, byte[] msg, byte[] iv, int block) { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); // 确认算法 if (isEn) { @@ -8142,9 +8243,7 @@ public class SydApi4j implements SydApi { packet.put("data", data); - RetWrap retWrap = repackEncrypt(packet, !isEn); - - return retWrap; + return repackEncrypt(packet, !isEn); } /** @@ -8164,8 +8263,8 @@ public class SydApi4j implements SydApi { public byte[] SM4EncryptDataCBC(int keyType, String key, int inType, int outType, byte[] msg) { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); byte[] iv = new byte[32]; Arrays.fill(iv, (byte) 0x30); @@ -8201,7 +8300,7 @@ public class SydApi4j implements SydApi { */ // 测试通过 // @Override - private boolean _SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) throws UnsupportedEncodingException { + private boolean _SM4DecryptFileDataCBC(int keyType, String key, int inType, int outType, String inFilePath, String outFilePath) { byte[] msg = FileUtil.getDataFromFile(inFilePath); @@ -8325,9 +8424,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("alg$option", 0x9); @@ -8362,8 +8461,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); byte[] iv = new byte[0 == nAlg ? 16 : 32]; Arrays.fill(iv, (byte) 0x30); @@ -8411,9 +8510,9 @@ public class SydApi4j implements SydApi { // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); switch (nAlg) { case 0: @@ -8448,8 +8547,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("7E"); Proto7E proto = new Proto7E(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8472,7 +8571,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -8504,7 +8605,7 @@ public class SydApi4j implements SydApi { } - static interface PackCallBack { + interface PackCallBack { void circle(int packSn, HashMap packet, Map ret); } @@ -8514,8 +8615,8 @@ public class SydApi4j implements SydApi { Proto8024 proto = new Proto8024(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8550,7 +8651,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8597,8 +8700,8 @@ public class SydApi4j implements SydApi { Proto8026 proto = new Proto8026(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8629,7 +8732,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8657,8 +8762,8 @@ public class SydApi4j implements SydApi { ProtoQ1 proto = new ProtoQ1(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8702,7 +8807,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8729,8 +8836,8 @@ public class SydApi4j implements SydApi { ProtoQ2 proto = new ProtoQ2(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8775,7 +8882,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8799,8 +8909,8 @@ public class SydApi4j implements SydApi { ProtoMAXF proto = new ProtoMAXF(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8813,9 +8923,11 @@ public class SydApi4j implements SydApi { data.put("TAK", TAK); - while (infoData.length() % 16 != 0) { - infoData += "00"; + StringBuilder infoDataBuilder = new StringBuilder(infoData); + while (infoDataBuilder.length() % 16 != 0) { + infoDataBuilder.append("00"); } + infoData = infoDataBuilder.toString(); data.put("MACData", infoData); // 渲染发送 @@ -8834,7 +8946,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8862,8 +8976,8 @@ public class SydApi4j implements SydApi { ProtoMU proto = new ProtoMU(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -8895,7 +9009,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -8935,7 +9051,7 @@ public class SydApi4j implements SydApi { // 获取协议 synchronized (this) { - int packNo = 0; +// int packNo = 0; for (int i = 0; i < loop; i++) { if (0 == i) { @@ -9003,7 +9119,7 @@ public class SydApi4j implements SydApi { } else { // 中间包 - packNo = 2; +// packNo = 2; byte[] msg = new byte[packSize]; System.arraycopy(data, i * packSize, msg, 0, packSize); println("\n\t中间包"); @@ -9033,8 +9149,8 @@ public class SydApi4j implements SydApi { ProtoMA proto = new ProtoMA(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9063,7 +9179,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9085,7 +9203,6 @@ public class SydApi4j implements SydApi { * @param fullMode 填充类型,0-弱填充 1 强填充 * @param infoData MAC数据 * @param mac MAC值 hex 格式 - * @return */ public void MACCheck(int alg, int keyType, String AK, int fullMode, byte[] infoData, String mac) { ProtoMC proto = new ProtoMC(); @@ -9102,8 +9219,8 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9133,7 +9250,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9141,7 +9260,6 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); } - return; } /** @@ -9159,8 +9277,8 @@ public class SydApi4j implements SydApi { ProtoYA proto = new ProtoYA(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9210,7 +9328,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9224,16 +9344,14 @@ public class SydApi4j implements SydApi { @Override public byte[] SYD_Encrypt_Data(int alg, String DEK, byte[] infoData, byte[] vector, int disperseAlg, byte[] factor) { String ss = SYD_Disperse(DEK, 0, 1, factor, disperseAlg, 0); - byte[] bytes = Encrypt_Data(alg, 1, 2, ss, infoData, vector); - return bytes; + return Encrypt_Data(alg, 1, 2, ss, infoData, vector); } @Override public byte[] SYD_Decrypt_Data(int alg, String DEK, byte[] infoData, byte[] vector, int disperseAlg, byte[] factor) { String ss = SYD_Disperse(DEK, 0, 1, factor, disperseAlg, 0); - byte[] bytes = Decrypt_Data(alg, 1, 2, ss, infoData, vector); - return bytes; + return Decrypt_Data(alg, 1, 2, ss, infoData, vector); } @@ -9256,8 +9374,7 @@ public class SydApi4j implements SydApi { ss = ss.substring(1, 17); } - byte[] bytes = MAC_Data(ss, infoData, IV); - return bytes; + return MAC_Data(ss, infoData, IV); } @@ -9266,6 +9383,7 @@ public class SydApi4j implements SydApi { try { InputStream is = SydApi.class.getClassLoader().getResourceAsStream("racal.sunyard.main.version"); + assert is != null; byte[] buff = new byte[is.available()]; is.read(buff); return new String(buff).trim(); @@ -9291,14 +9409,14 @@ public class SydApi4j implements SydApi { // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - String keyTpye = ""; + String keyTpye; if (0 == Alg) { data.put("alg$option", 0); @@ -9342,7 +9460,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9369,14 +9490,14 @@ public class SydApi4j implements SydApi { Proto4D43 proto = new Proto4D43(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); - String keyTpye = ""; + String keyTpye; if (0 == Alg) { data.put("alg$option", 0); @@ -9422,7 +9543,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9443,8 +9567,8 @@ public class SydApi4j implements SydApi { public RetWrap getStatus() { ProtoHI proto = new ProtoHI(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9465,7 +9589,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection cpu = (PacketSection) ret.get("cpu"); PacketSection mem = (PacketSection) ret.get("mem"); PacketSection disk = (PacketSection) ret.get("disk"); @@ -9492,8 +9618,8 @@ public class SydApi4j implements SydApi { public RetWrap genAndPrint(String keyType, String solution) { ProtoA2 proto = new ProtoA2(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9520,7 +9646,9 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9550,8 +9678,8 @@ public class SydApi4j implements SydApi { public void setPrintFormat(String format) { ProtoPA proto = new ProtoPA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9575,7 +9703,9 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9585,7 +9715,6 @@ public class SydApi4j implements SydApi { throw new SydApiException(error.getInfo(), error.autoInt()); } - return; } @Override @@ -9593,8 +9722,8 @@ public class SydApi4j implements SydApi { ProtoNE proto = new ProtoNE(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9624,7 +9753,10 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9661,8 +9793,8 @@ public class SydApi4j implements SydApi { ProtoA8 proto = new ProtoA8(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9690,7 +9822,9 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9719,8 +9853,8 @@ public class SydApi4j implements SydApi { ProtoA6 proto = new ProtoA6(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9748,7 +9882,9 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9787,8 +9923,8 @@ public class SydApi4j implements SydApi { ProtoMA proto = new ProtoMA(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9817,7 +9953,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9853,8 +9992,8 @@ public class SydApi4j implements SydApi { ProtoB0 proto = new ProtoB0(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -9879,7 +10018,10 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -9912,8 +10054,8 @@ public class SydApi4j implements SydApi { public RetWrap changZMK2LMK(String zmki, String zmk) { ProtoBY proto = new ProtoBY(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -9932,13 +10074,16 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -9982,8 +10127,8 @@ public class SydApi4j implements SydApi { public RetWrap getVersion(String num) { ProtoHV proto = new ProtoHV(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -10007,7 +10152,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection name = (PacketSection) ret.get("name"); PacketSection model = (PacketSection) ret.get("model"); PacketSection equipNo = (PacketSection) ret.get("equipNo"); @@ -10037,8 +10184,8 @@ public class SydApi4j implements SydApi { */ public RetWrap calcHash(String key, int hashType, String contextInfo, byte[] infoData) { Proto82 proto = new Proto82(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10054,13 +10201,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10092,9 +10241,9 @@ public class SydApi4j implements SydApi { Proto68 proto = new Proto68(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("DEK", DEK.getBytes()); @@ -10123,7 +10272,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10148,9 +10299,9 @@ public class SydApi4j implements SydApi { Proto69 proto = new Proto69(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("alg$option", 1); data.put("fillKey$option", 0); @@ -10179,7 +10330,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10204,9 +10357,9 @@ public class SydApi4j implements SydApi { Proto62 proto = new Proto62(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("keyType", keyType); data.put("key", key); @@ -10232,7 +10385,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10257,9 +10412,9 @@ public class SydApi4j implements SydApi { Proto63 proto = new Proto63(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("keyType", keyType); data.put("key", key); @@ -10286,12 +10441,12 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); - if (!"00".equals(error.getString())) { - return false; - } - return true; + return "00".equals(error.getString()); } /** @@ -10309,9 +10464,9 @@ public class SydApi4j implements SydApi { Proto64 proto = new Proto64(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("keyType", keyType); data.put("key", key); @@ -10338,7 +10493,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; + PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10365,9 +10523,9 @@ public class SydApi4j implements SydApi { Proto65 proto = new Proto65(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("keyType", keyType); data.put("srcKey", srcKey); @@ -10393,7 +10551,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10417,9 +10577,9 @@ public class SydApi4j implements SydApi { Proto66 proto = new Proto66(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("key", key); data.put("AlgorithmKey", "P"); @@ -10444,7 +10604,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10467,9 +10629,9 @@ public class SydApi4j implements SydApi { Proto67 proto = new Proto67(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("srcKey", srcKey); data.put("aimKey", aimKey); @@ -10494,12 +10656,14 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } - RetWrap rw = new RetWrap(); +// RetWrap rw = new RetWrap(); return ((PacketSection) ret.get("info")).getBytes(); } @@ -10515,9 +10679,9 @@ public class SydApi4j implements SydApi { Proto60 proto = new Proto60(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); // 渲染发送 耗时长 @@ -10535,7 +10699,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10560,9 +10726,9 @@ public class SydApi4j implements SydApi { Proto71 proto = new Proto71(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("publicKey", pk); data.put("key", key); @@ -10584,7 +10750,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10607,8 +10775,8 @@ public class SydApi4j implements SydApi { //ProtocolRender render = pset.getProtocolReqRender("E2"); ProtoE4 proto = new ProtoE4(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -10629,7 +10797,7 @@ public class SydApi4j implements SydApi { //ProtocolParser parser = pset.getProtocolResParse(); - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); @@ -10639,7 +10807,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10659,7 +10829,7 @@ public class SydApi4j implements SydApi { */ public byte[] itHash(int digestAlg, byte[] orgData) { //同步加密机与裸签摘要算法枚举值 - if(!(digestAlg==1||digestAlg==2||digestAlg==3)){ + if (!(digestAlg == 1 || digestAlg == 2 || digestAlg == 3)) { throw new IllegalArgumentException("不支持的算法:" + digestAlg); } int hashType = 0; @@ -10714,8 +10884,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap countHash(int hashType, int packSn, byte[] msg, byte[] filterData) { Proto7D proto = new Proto7D(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10730,13 +10900,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10755,16 +10927,15 @@ public class SydApi4j implements SydApi { /** * 10.1装载对称密钥至用户存储区(=) -> pass * - * @param index 索引地址,指示用户密钥存储区的27位地址。 - * @param key 被加密的密钥 - * @param checkValue 校验值 + * @param index 索引地址,指示用户密钥存储区的27位地址。 + * @param key 被加密的密钥 * @return 是否成功 */ // 测试通过 - public Boolean loadKey(String index, String key, String checkValue) { + public Boolean loadKey(String index, String key) { ProtoLB proto = new ProtoLB(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10795,13 +10966,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10820,8 +10993,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap readKey(String index) { ProtoLF proto = new ProtoLF(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10835,13 +11008,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10865,13 +11040,12 @@ public class SydApi4j implements SydApi { * @param index 索引地址,指示用户密钥存储区的27位地址 * @param key 被加密的密钥 * @param checkValue 校验值 - * @return */ // 测试通过 public Boolean updateKey(String index, String key, String checkValue) { ProtoLG proto = new ProtoLG(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10908,13 +11082,16 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10941,8 +11118,8 @@ public class SydApi4j implements SydApi { // 测试通过 public byte[] switchEncryY(String srcType, int sca, String desKeyType, int dca, int roundModel, String sDEK, String dDEK, byte[] infoData, String vector) { ProtoY0 proto = new ProtoY0(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -10965,13 +11142,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -10985,10 +11164,6 @@ public class SydApi4j implements SydApi { /** * 自动 4k 分包 * - * @param keyType - * @param mode - * @param key - * @param msg * @return MAC 值 */ public String genMAB(int mode, int keyType, String key, byte[] msg) { @@ -11003,8 +11178,8 @@ public class SydApi4j implements SydApi { return new String((byte[]) retWrap.get("MAB")); } - int loop = 0; - int tailLen = 0; + int loop; + int tailLen; loop = msg.length / blockSize; tailLen = msg.length % blockSize; @@ -11015,7 +11190,7 @@ public class SydApi4j implements SydApi { // loop 至少是 2 // 分包数据 - byte[] data = null; + byte[] data; // 首包 // 必然有 @@ -11060,8 +11235,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genMAB(int packSn, int keyType, int mode, String key, byte[] iv, byte[] msg) { ProtoMS proto = new ProtoMS(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11082,13 +11257,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11109,8 +11286,8 @@ public class SydApi4j implements SydApi { // 测试通过 public String genPIN(String account) { ProtoJA proto = new ProtoJA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11126,13 +11303,16 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11149,14 +11329,13 @@ public class SydApi4j implements SydApi { * @param account 账号,账号中去除校验位的最右 12 位 * @param encryptionAlg 加密算法 0:DES, 1:3DES(默认), 2:SM4 * @param pinBlock PIN 块,LMK 对(02-03)下加密的导出 PINBLOCK。(PINBLOCK格式默认为 01 格式) - * @return */ public Boolean decryPINBAndPrint(char fileType, String account, int encryptionAlg, String pinBlock) { ProtoPE proto = new ProtoPE(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11188,7 +11367,10 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11206,8 +11388,6 @@ public class SydApi4j implements SydApi { * @param zpk2 目的 ZPK,将要加密 PIN 块的 ZPK;LMK 对(06-07)下加密 * @param sca 源算法 SCA,1:3DES, 2:SM4 * @param dca 目的算法DCA, 1:3DES, 2:SM4 - * @param spt - * @param dpt * @param pin 源 PIN 块,源 ZPK 下加密的源 PIN 块 * @param account 账号中去除校验位的最右 12 位 * @return 目的 ZPK 下加密的目的 PIN 块 @@ -11219,8 +11399,8 @@ public class SydApi4j implements SydApi { ProtoCC proto = new ProtoCC(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11253,7 +11433,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11282,8 +11465,8 @@ public class SydApi4j implements SydApi { ProtoJE proto = new ProtoJE(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11313,7 +11496,10 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11340,8 +11526,8 @@ public class SydApi4j implements SydApi { ProtoJG proto = new ProtoJG(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11371,7 +11557,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11400,8 +11588,8 @@ public class SydApi4j implements SydApi { ProtoCA proto = new ProtoCA(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11434,7 +11622,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11462,8 +11652,8 @@ public class SydApi4j implements SydApi { ProtoJC proto = new ProtoJC(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11493,7 +11683,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11518,8 +11710,8 @@ public class SydApi4j implements SydApi { ProtoBA proto = new ProtoBA(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11544,7 +11736,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11570,8 +11764,8 @@ public class SydApi4j implements SydApi { ProtoNG proto = new ProtoNG(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11596,7 +11790,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -11623,8 +11819,8 @@ public class SydApi4j implements SydApi { */ public String transPinFrOneKeyToAno(String srcKeyType, String desKeyType, String srcKey, String desKey, int sca, int dca, String sPin, String primaryAccount) { ProtoCB proto = new ProtoCB(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -11648,14 +11844,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); - PacketSection error = (PacketSection) ret.get("error"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { @@ -11676,8 +11873,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genTAK(String tmk) { ProtoHA proto = new ProtoHA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11695,13 +11892,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11738,8 +11937,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap transTAKFromZMKToLMK(String zmk, String tak) { ProtoMF proto = new ProtoMF(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11758,13 +11957,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11793,8 +11994,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap transTAKFromLMKToZMK(String zmk, String tak) { ProtoMG proto = new ProtoMG(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11813,13 +12014,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -11848,8 +12051,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap transTAKFromLMKToTMK(String tmk, String tak) { ProtoAG proto = new ProtoAG(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11868,7 +12071,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -11904,8 +12107,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genKeyByTMKOrTPKOrPVK(String tmk) { ProtoHC proto = new ProtoHC(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11924,7 +12127,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -11968,8 +12171,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranKeyByTMKToAno(String tmkNow, String tmkStorage) { ProtoAE proto = new ProtoAE(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -11988,7 +12191,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12023,8 +12226,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranKeyByTMKFromZMKToLMK(String zmk, String tmk) { ProtoFC proto = new ProtoFC(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12044,7 +12247,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12079,8 +12282,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranKeyByTMKFromLMKToZMK(String zmk, String tmk) { ProtoFE proto = new ProtoFE(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12100,13 +12303,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12136,8 +12341,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genZEKOrZAK(int key, String zmk) { ProtoFI proto = new ProtoFI(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12157,13 +12362,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12217,8 +12424,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZEAKFromZMKToLMK(int key, String zmk, String zekOrzak) { ProtoFK proto = new ProtoFK(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12239,13 +12446,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12284,8 +12493,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZEAKFromLMKToZMK(int key, String zmk, String zekOrzak) { ProtoFM proto = new ProtoFM(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12306,7 +12515,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12350,8 +12559,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap genZPK(String zmk) { ProtoIA proto = new ProtoIA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12370,13 +12579,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12414,8 +12625,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZPKFromZMKToLMK(String zmk, String zpk) { ProtoFA proto = new ProtoFA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12435,7 +12646,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -12471,8 +12682,8 @@ public class SydApi4j implements SydApi { // 测试通过 public RetWrap tranZPKFromLMKToZMK(String zmk, String zpk) { ProtoGC proto = new ProtoGC(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12492,13 +12703,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12526,8 +12739,8 @@ public class SydApi4j implements SydApi { public RetWrap genZMKAndPrint() { ProtoOC proto = new ProtoOC(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -12556,7 +12769,9 @@ public class SydApi4j implements SydApi { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ( @@ -12589,8 +12804,8 @@ public class SydApi4j implements SydApi { public RetWrap composeKey(String keyType, String[] keys) { ProtoA4 proto = new ProtoA4(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); int len = 0; for (String key : keys) { @@ -12623,13 +12838,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12658,8 +12875,8 @@ public class SydApi4j implements SydApi { */ public RetWrap roleVerify(String roleInfo) { Proto61 proto = new Proto61(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); @@ -12672,13 +12889,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12700,9 +12919,9 @@ public class SydApi4j implements SydApi { Proto73 proto = new Proto73(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); //String sk = "00010000C530EFBC1971D6526BDAB640370EE9197BFB0D78F88417C1ED49981FFEF226980000000000000000000000000000000000000000000000000000000000000000"; //data.put("privateKey", Util.hexString2Bytes(sk)); @@ -12727,7 +12946,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12758,9 +12979,9 @@ public class SydApi4j implements SydApi { ProtoAB proto = new ProtoAB(); // 填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("keyType", "002"); data.put("key", key.getBytes()); @@ -12784,7 +13005,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -12835,24 +13058,22 @@ public class SydApi4j implements SydApi { @Override public String SYMEncrytByKeyIndex(String index, String intPutData) { RetWrap retWrap = this.SYMEnDeData(true, 1, new String((byte[]) this.readKey(index).get("key")), 1, 1, intPutData.getBytes(), null, 0); - String msg_all_en = Util.bytes2HexString((byte[]) retWrap.get("msg_all")); - return msg_all_en; + return Util.bytes2HexString((byte[]) retWrap.get("msg_all")); } @Override public String SYMDecrytByKeyIndex(String index, String intPutData) { RetWrap retWrap = this.SYMEnDeData(false, 1, new String((byte[]) this.readKey(index).get("key")), 1, 1, Util.hexString2Bytes(intPutData), null, 0); - String msg_all_de = new String(Util.hexString2Bytes(Util.bytes2HexString((byte[]) retWrap.get("msg_all")))); - return msg_all_de; + return new String(Util.hexString2Bytes(Util.bytes2HexString((byte[]) retWrap.get("msg_all")))); } // 测试通过 @Override public boolean setDevCert(String equipType, String equipNo, byte[] equipSk, byte[] equipPk, byte[] manuPk, byte[] authSign) { Proto90 proto = new Proto90(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -12874,7 +13095,9 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if ("00".equals(error.getString())) { return true; @@ -12888,8 +13111,8 @@ public class SydApi4j implements SydApi { @Override public boolean changeDevCert(byte[] equipSk, byte[] equipPk, byte[] authSign) { Proto91 proto = new Proto91(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -12908,7 +13131,9 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if ("00".equals(error.getString())) { return true; @@ -12931,8 +13156,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap getDeviceCertiInfo() { Proto92 proto = new Proto92(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -12953,7 +13178,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); RetWrap retWrap = new RetWrap(); if ("00".equals(error.getString())) { @@ -12976,8 +13203,8 @@ public class SydApi4j implements SydApi { @Override public String deviceSignC(byte[] orgData) { Proto97 proto = new Proto97(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13009,8 +13236,8 @@ public class SydApi4j implements SydApi { public boolean setToken(byte[] token) { Proto98 proto = new Proto98(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13027,7 +13254,9 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if ("00".equals(error.getString())) { return true; @@ -13041,8 +13270,8 @@ public class SydApi4j implements SydApi { @Override public byte[] calcAuthHead(byte[] certiContent) { Proto99 proto = new Proto99(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13064,7 +13293,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; // PacketSection equipType = (PacketSection) ret.get( "equipType" ); // PacketSection equipNo = (PacketSection) ret.get( "equipNo" ); // RetWrap retWrap = new RetWrap(); @@ -13099,7 +13330,7 @@ public class SydApi4j implements SydApi { } private RetWrap attachedVerifySoftGetAll(byte[] sign) { - RetWrap ret = null; + RetWrap ret; try { ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(new String(sign)); @@ -13128,7 +13359,7 @@ public class SydApi4j implements SydApi { } private boolean attachedVerifySoft(String sign) { - RetWrap ret = null; + RetWrap ret; try { ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); } catch (IOException e) { @@ -13144,7 +13375,7 @@ public class SydApi4j implements SydApi { } public boolean attachedVerifyRSA(int rFlag, String sign) { - RetWrap ret = null; + RetWrap ret; boolean verifyRSA = false; try { ret = ASN1Util.takeOutOriginDataFromPKCS7Sign(sign); @@ -13158,11 +13389,11 @@ public class SydApi4j implements SydApi { // byte[] hash = this.SM3Hash( Util.bytes2HexString( pk ), orgData ); String sign2 = (String) ret.get("sign"); if (orgData.length < 4096) { - verifyRSA= this.detachedVerify(rFlag, 1, orgData, sign2); - }else { - // RetWrap hashData = this.countHash(0, 0, orgData, null); + verifyRSA = this.detachedVerify(rFlag, 1, orgData, sign2); + } else { + // RetWrap hashData = this.countHash(0, 0, orgData, null); byte[] hashData = itHash(rFlag, orgData); - verifyRSA= this.detachedVerify(rFlag, 0, hashData, sign2); + verifyRSA = this.detachedVerify(rFlag, 0, hashData, sign2); } return verifyRSA; } @@ -13179,50 +13410,50 @@ public class SydApi4j implements SydApi { @Override public String attachedSign(byte[] orgData, String sCertDN) { try { - if ( orgData.length > 4096 ) { - return attachedSignSoft( orgData, sCertDN ); + if (orgData.length > 4096) { + return attachedSignSoft(orgData, sCertDN); } else { - return attachedSignHsm( orgData, sCertDN ); + return attachedSignHsm(orgData, sCertDN); } - }catch (UnsupportedEncodingException e) { + } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("DN is not in GBK"); } } @Override - public String attachedSignRSA(int rFlag,byte[] orgData, String sCertDN) { + public String attachedSignRSA(int rFlag, byte[] orgData, String sCertDN) { //return attachedSignRSA( rFlag, orgData, sCertDN ); try { - if ( orgData.length > 4096 ) { - return attachedSignRSASoft( rFlag,orgData, sCertDN ); + if (orgData.length > 4096) { + return attachedSignRSASoft(rFlag, orgData, sCertDN); } else { - return attachedSignRSAHsm( rFlag,orgData, sCertDN ); + return attachedSignRSAHsm(rFlag, orgData, sCertDN); } - }catch (UnsupportedEncodingException e) { + } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("DN is not in GBK"); } } - public String attachedSignRSASoft(int rFlag,byte[] orgData, String sCertDN) throws UnsupportedEncodingException { - sCertDN = verifyDn( sCertDN ); + public String attachedSignRSASoft(int rFlag, byte[] orgData, String sCertDN) throws UnsupportedEncodingException { + sCertDN = verifyDn(sCertDN); try { // RetWrap hashData = countHash(0, 0, orgData, null); // byte[] hash = (byte[]) hashData.get("hash"); byte[] hash = itHash(rFlag, orgData); - String sign = this.detachedSign(rFlag,0, hash, sCertDN); - return ASN1Util.addOriginDataToPKCS7Sign( orgData, sign ); + String sign = this.detachedSign(rFlag, 0, hash, sCertDN); + return ASN1Util.addOriginDataToPKCS7Sign(orgData, sign); } catch (IOException e) { e.printStackTrace(); throw new IllegalArgumentException("解析 pkcs7 签名格式时错误"); } } - public String attachedSignRSAHsm(int rFlag,byte[] orgData, String sCertDN) { - sCertDN = verifyDn( sCertDN ); + public String attachedSignRSAHsm(int rFlag, byte[] orgData, String sCertDN) { + sCertDN = verifyDn(sCertDN); Proto8018 proto = new Proto8018(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -13233,11 +13464,12 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); @@ -13269,7 +13501,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -13281,13 +13515,11 @@ public class SydApi4j implements SydApi { } - - public String attachedSignHsm(byte[] orgData, String sCertDN) { - sCertDN = verifyDn( sCertDN ); + sCertDN = verifyDn(sCertDN); Proto8018 proto = new Proto8018(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); PacketSN sn = PacketSN.gen(); byte[] dn = new byte[256]; @@ -13298,11 +13530,12 @@ public class SydApi4j implements SydApi { e.printStackTrace(); } + assert s != null; System.arraycopy(s, 0, dn, 0, s.length); // 填充数据 - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); packet.put("data", data); packet.put("header", sn.getSn().array()); data.put("DN", dn); @@ -13324,7 +13557,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { @@ -13336,15 +13571,15 @@ public class SydApi4j implements SydApi { } public boolean attachedVerifyHsm(String sign) { - return attachedVerify( sign.getBytes() ); + return attachedVerify(sign.getBytes()); } @Override public boolean attachedVerify(String sign) { - if ( sign.length() > 4096 ) { - return attachedVerifySoft( sign ); + if (sign.length() > 4096) { + return attachedVerifySoft(sign); } else { - return attachedVerify( sign.getBytes() ); + return attachedVerify(sign.getBytes()); } } @@ -13353,8 +13588,8 @@ public class SydApi4j implements SydApi { Proto8019 proto = new Proto8019(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); PacketSN sn = PacketSN.gen(); packet.put("data", data); @@ -13380,12 +13615,14 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { SydApiException e = new SydApiException(error.getInfo(), error.autoInt()); - println("验签失败=" + e.toString()); + println("验签失败=" + e); return false; } @@ -13395,7 +13632,7 @@ public class SydApi4j implements SydApi { @Override public byte[] attachedVerifyRD(String sign) { - return attachedVerifyRD( sign.getBytes() ); + return attachedVerifyRD(sign.getBytes()); } @Override @@ -13415,11 +13652,11 @@ public class SydApi4j implements SydApi { * @return 返回CERT_INFO结构的证书数据 */ public X509 attachedVerifyAndGetX509(String sign) { - return attachedVerifyAndGetX509( sign.getBytes() ); + return attachedVerifyAndGetX509(sign.getBytes()); } public X509 attachedVerifyAndGetX509(byte[] sign) { - RetWrap retWrap = attachedVerifyAndGetAll( sign); + RetWrap retWrap = attachedVerifyAndGetAll(sign); if (retWrap == null) return null; @@ -13427,17 +13664,17 @@ public class SydApi4j implements SydApi { } public RetWrap attachedVerifyAndGetAll(String sign) { - return attachedVerifyAndGetAll( sign.getBytes() ); + return attachedVerifyAndGetAll(sign.getBytes()); } // 大数据兼容处理 public RetWrap attachedVerifyAndGetAll(byte[] sign) { - if( sign.length > 4096 ) { + if (sign.length > 4096) { - return attachedVerifySoftGetAll( sign ); + return attachedVerifySoftGetAll(sign); } else { - return attachedVerifyAndGetAllHsm( sign ); + return attachedVerifyAndGetAllHsm(sign); } } @@ -13446,8 +13683,8 @@ public class SydApi4j implements SydApi { Proto8019 proto = new Proto8019(); // 填充数据 - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13471,13 +13708,16 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); byte[] be = error.getBytes(); if ((0 != (be[0] ^ be[1]))) { return null; } + PacketSection info = (PacketSection) ret.get("info"); byte[] pCertInfo = info.getBytes(); @@ -13492,7 +13732,7 @@ public class SydApi4j implements SydApi { RetWrap retWrap = new RetWrap(); retWrap.put("x509", x509); - retWrap.put("oData", oDataB ); + retWrap.put("oData", oDataB); return retWrap; } @@ -13513,15 +13753,15 @@ public class SydApi4j implements SydApi { * @param inputData 输入数据 * @return byte[] 数据 * @author wp - * @Description:(A0)对称加密接口 + * @Description:(A0) 对称加密接口 * @date 2021/5/31 */ // 测试通过 @Override public byte[] SYMEncryt(int storageType, String key, int algFlag, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData) { ProtoS0 proto = new ProtoS0(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13541,7 +13781,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); data.put("keyIndex", "0000000000000000000" + gId); } //算法类型支持算法:3DES、AES、SM1、SM4 @@ -13585,7 +13825,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { @@ -13617,7 +13859,7 @@ public class SydApi4j implements SydApi { } if (0 == storageType) { String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); // 密钥索引 bb.put(("0000000000000000000" + gId).getBytes()); } @@ -13717,8 +13959,8 @@ public class SydApi4j implements SydApi { @Override public byte[] SYMDecryt(int storageType, String key, int algFlag, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData) { ProtoS1 proto = new ProtoS1(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13738,7 +13980,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); data.put("keyIndex", "0000000000000000000" + gId); } //算法类型支持算法:3DES、AES、SM1、SM4 @@ -13782,8 +14024,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); - + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -13813,8 +14056,8 @@ public class SydApi4j implements SydApi { @Override public byte[] genMAC(int storageType, String key, int algFlag, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData, int macDataLen) { ProtoS2 proto = new ProtoS2(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -13834,7 +14077,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); data.put("keyIndex", "0000000000000000000" + gId); } //算法类型 @@ -13881,8 +14124,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); - + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -13913,7 +14157,7 @@ public class SydApi4j implements SydApi { } if (0 == storageType) { String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); // 密钥索引 bb.put(("0000000000000000000" + gId).getBytes()); } @@ -14009,8 +14253,8 @@ public class SydApi4j implements SydApi { @Override public byte[] genMacAndNextIv(String keyIndex, byte[] iv, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] inputData) { ProtoS3 proto = new ProtoS3(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14069,8 +14313,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); - + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14182,8 +14427,8 @@ public class SydApi4j implements SydApi { @Override public RetWrap genSYMkey(int storageType, int algFlag, int keyLength, String keyIndex) { ProtoS4 proto = new ProtoS4(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14208,7 +14453,9 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14235,8 +14482,8 @@ public class SydApi4j implements SydApi { @Override public byte[] signData(int storageType, int algFlag, String key, byte[] inputData) { ProtoS5 proto = new ProtoS5(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14258,7 +14505,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); data.put("keyIndex", "0000000000000000000" + gId); } @@ -14270,13 +14517,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14301,8 +14550,8 @@ public class SydApi4j implements SydApi { @Override public Boolean verifySignData(int storageType, int algFlag, String key, byte[] orgData, byte[] signData) { ProtoS6 proto = new ProtoS6(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14324,7 +14573,7 @@ public class SydApi4j implements SydApi { // throw new SydApiException("输入的数据格式错误", 21); // } String gId = "00000000" + key; - gId = gId.substring(gId.length() - 8, gId.length()); + gId = gId.substring(gId.length() - 8); data.put("keyIndex", "0000000000000000000" + gId); } data.put("orgData", orgData); @@ -14336,7 +14585,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -14366,8 +14615,8 @@ public class SydApi4j implements SydApi { @Override public byte[] ASYMEnData(int storageType, int algFlag, String key, byte[] inputData) { ProtoS7 proto = new ProtoS7(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14401,7 +14650,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -14431,8 +14680,8 @@ public class SydApi4j implements SydApi { @Override public byte[] ASYMDeData(int storageType, int algFlag, String key, byte[] cipherText) { ProtoS8 proto = new ProtoS8(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14466,7 +14715,7 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { @@ -14528,18 +14777,18 @@ public class SydApi4j implements SydApi { if (protectedKeyDivNum != 0) { // 分散因子 - for (int i = 0; i < protectedKeyDivData.size(); i++) { - bb.put(protectedKeyDivData.get(i)); + for (byte[] protectedKeyDivDatum : protectedKeyDivData) { + bb.put(protectedKeyDivDatum); } // 分散算法标识 - for (int i = 0; i < protectedKeyDivAlgFlag.length; i++) { - bb.put(String.format("%02d", MyUtil.transAlg(protectedKeyDivAlgFlag[i])).getBytes()); + for (int j : protectedKeyDivAlgFlag) { + bb.put(String.format("%02d", MyUtil.transAlg(j)).getBytes()); } // 分散算法的初始向量 - for (int i = 0; i < protectedKeyDivIv.size(); i++) { - bb.put(protectedKeyDivIv.get(i)); + for (byte[] bytes : protectedKeyDivIv) { + bb.put(bytes); } } @@ -14614,8 +14863,8 @@ public class SydApi4j implements SydApi { private RetWrap _exportKeyAndCalc(String protectedKeyIndex, int algFlag, byte[] iv, int protectedKeyDivNum, List protectedKeyDivData, int[] protectedKeyDivAlgFlag, List protectedKeyDivIv, int exportKeyNum, List structList) { ProtoS9 proto = new ProtoS9(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14663,14 +14912,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); - PacketSection error = (PacketSection) ret.get("error"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); } else { @@ -14700,8 +14950,8 @@ public class SydApi4j implements SydApi { public byte[] consultKey(String keyIndex, int divNum, List divData, int[] divAlgFlag, List divIv, byte[] clientRandom, byte[] serverRandom) { ProtoSA proto = new ProtoSA(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14748,13 +14998,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14777,8 +15029,8 @@ public class SydApi4j implements SydApi { @Override public byte[] pHash(String key, String seed, int outLen) { ProtoSB proto = new ProtoSB(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14792,13 +15044,15 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14835,8 +15089,8 @@ public class SydApi4j implements SydApi { String encryptKeyIndex, int encryptAlgFlag, byte[] encryptIv, int encryptKeyDivNum, List encryptKeyDivData, int[] encryptKeyDivAlgFlag, List encryptKeyDivIv, byte[] inputData) { ProtoSC proto = new ProtoSC(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14924,8 +15178,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); - + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -14950,8 +15205,8 @@ public class SydApi4j implements SydApi { @Override public byte[] ASYMDecryptAndEn(int storageType, String key, int decryptAlgFlag, byte[] inputData, int prefixIgnoreCount, int suffixIgnoreCount) { ProtoSD proto = new ProtoSD(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -14985,7 +15240,9 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15009,8 +15266,8 @@ public class SydApi4j implements SydApi { @Override public byte[] SYMDecryptAndEn(String decryptKey, int decryptAlgFlag, byte[] decryptIv, byte[] inputData) { ProtoSE proto = new ProtoSE(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15031,7 +15288,10 @@ public class SydApi4j implements SydApi { if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15060,8 +15320,8 @@ public class SydApi4j implements SydApi { public byte[] decryptByMainAndSYMEn(String encryptKeyIndex, int encryptAlgFlag, byte[] encryptIv, int encryptKeyDivNum, List encryptKeyDivData, int[] encryptKeyDivAlgFlag, List encryptKeyDivIv, byte[] inputData) { ProtoSF proto = new ProtoSF(); - HashMap packet = new HashMap(); - HashMap data = new HashMap(); + HashMap packet = new HashMap<>(); + HashMap data = new HashMap<>(); packet.put("data", data); PacketSN sn = PacketSN.gen(); packet.put("header", sn.getSn().array()); @@ -15100,13 +15360,16 @@ public class SydApi4j implements SydApi { bb = syncRead(syncSend(bb)); } - Map ret = null; + Map ret; Packet p = proto.parse(bb, packet); ret = p.toMap(); if (!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())) { throw new SydApiException("包序号不匹配", -4); } else { - ret = (Map) ret.get("data"); +// ret = (Map) ret.get("data"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); @@ -15123,9 +15386,9 @@ public class SydApi4j implements SydApi { //填充数据 PacketSN sn = PacketSN.gen(); - HashMap packet = new HashMap(); + HashMap packet = new HashMap<>(); packet.put("header", sn.getSn().array()); - HashMap data = new HashMap(); + HashMap data = new HashMap<>(); data.put("nRandomSize", nRandomSize); packet.put("data", data); @@ -15143,8 +15406,9 @@ public class SydApi4j implements SydApi { ) { throw new SydApiException("包序号不匹配", -4); } - ret = (Map) ret.get("data"); - PacketSection error = (PacketSection) ret.get("error"); + @SuppressWarnings("unchecked") + Map dataMap = (Map) ret.get("data"); + ret = dataMap; PacketSection error = (PacketSection) ret.get("error"); if (!"00".equals(error.getString())) { throw new SydApiException(error.getInfo(), error.autoInt()); }