From f77b5daa93968d6b2a653f3038459b1b6c3be782 Mon Sep 17 00:00:00 2001 From: "junj2.liang" Date: Mon, 5 Sep 2022 10:22:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/sunyard/sge/database/SydApi.java | 2 +- .../sunyard/sge/database/SydApi4Database.java | 24 +-- .../com/sunyard/sge/database/SydApiSM4.java | 189 ++++++++---------- .../com/sunyard/sge/database/SydApiSM423.java | 186 +++++++++++++++++ .../com/sunyard/sge/log/SydLogFactory.java | 2 + src/test/java/test/LongDataTest.java | 182 +++++++---------- 6 files changed, 356 insertions(+), 229 deletions(-) create mode 100644 src/main/java/com/sunyard/sge/database/SydApiSM423.java diff --git a/src/main/java/com/sunyard/sge/database/SydApi.java b/src/main/java/com/sunyard/sge/database/SydApi.java index 9bf698c..cf835d2 100644 --- a/src/main/java/com/sunyard/sge/database/SydApi.java +++ b/src/main/java/com/sunyard/sge/database/SydApi.java @@ -272,7 +272,7 @@ public interface SydApi { ); - public SydApiSM4 initSM4( int iKeyIndex, int ende); + public SydApiSM423 initSM4(int iKeyIndex, int ende); public SydApiSM3 initSM3(); diff --git a/src/main/java/com/sunyard/sge/database/SydApi4Database.java b/src/main/java/com/sunyard/sge/database/SydApi4Database.java index 405f447..37d6c70 100644 --- a/src/main/java/com/sunyard/sge/database/SydApi4Database.java +++ b/src/main/java/com/sunyard/sge/database/SydApi4Database.java @@ -3,14 +3,12 @@ package com.sunyard.sge.database; import com.sunyard.SydApiException; import com.sunyard.proto.Util; import com.sunyard.sge.database.pool.HsmLinkInfo; -import com.sunyard.sge.database.pool.SydApiPool; import com.sunyard.sge.log.LogFactory; import com.sunyard.sge.pool.ObjectInPool; import com.sunyard.util.SYMUtil; import org.apache.logging.log4j.Logger; import racal.sunyard.main.SydApi4j; -import javax.annotation.CheckForNull; import java.util.Arrays; import java.util.List; @@ -132,7 +130,7 @@ public class SydApi4Database implements SydApi { if (null == this.hsms || this.hsms.length < 1) { throw new SydApiException(-1); } - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { @@ -191,7 +189,7 @@ public class SydApi4Database implements SydApi { } - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { @@ -254,7 +252,7 @@ public class SydApi4Database implements SydApi { updateLastUseTime(); String keyIndex = String.format("%08d", iKeyIndex); - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; @@ -335,7 +333,7 @@ public class SydApi4Database implements SydApi { keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); } - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; @@ -394,7 +392,7 @@ public class SydApi4Database implements SydApi { updateLastUseTime(); String keyIndex = String.format("%08d", iKeyIndex); - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; @@ -477,7 +475,7 @@ public class SydApi4Database implements SydApi { for (int i = 0; i < iKeyIndex.length; i++) { keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); } - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; @@ -545,7 +543,7 @@ public class SydApi4Database implements SydApi { keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i])); } - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); for (SydApi4j api : this.hsms) { if (null == api) { continue; @@ -615,7 +613,7 @@ public class SydApi4Database implements SydApi { if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) { iv = pcProcData.getBytes(); } - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { @@ -714,7 +712,7 @@ public class SydApi4Database implements SydApi { String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex)); log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}", keyIndex); - SydApiException le = new SydApiException(0); + SydApiException le = new SydApiException(-1); updateLastUseTime(); for (SydApi4j api : this.hsms) { if (null == api) { @@ -775,9 +773,9 @@ public class SydApi4Database implements SydApi { @Override - public SydApiSM4 initSM4(int iKeyIndex, int ende) { + public SydApiSM423 initSM4(int iKeyIndex, int ende) { - return new SydApiSM4(this,iKeyIndex,ende); + return new SydApiSM423(this,iKeyIndex,ende); } public SydApiSM3 initSM3() { diff --git a/src/main/java/com/sunyard/sge/database/SydApiSM4.java b/src/main/java/com/sunyard/sge/database/SydApiSM4.java index 725d4f0..e542423 100644 --- a/src/main/java/com/sunyard/sge/database/SydApiSM4.java +++ b/src/main/java/com/sunyard/sge/database/SydApiSM4.java @@ -2,6 +2,7 @@ package com.sunyard.sge.database; import com.sunyard.SydApiException; import com.sunyard.proto.Util; +import com.sunyard.sge.bytes.BytesQueue; import java.nio.ByteBuffer; @@ -14,8 +15,7 @@ public class SydApiSM4 { private int iFlag; private boolean is_finished = false; private byte[] dataResult = new byte[0]; - - ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]); + BytesQueue queue = new BytesQueue(); public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) { @@ -38,115 +38,93 @@ public class SydApiSM4 { throw new SydApiException(0); } if (iFlag == 1) { - if (0 == sourceWrap.array().length) { + if (0 == queue.getLength()) { //加密 if (plength < 16) { - ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); - bufferResult.put(sourceWrap); - bufferResult.put(pcData); - sourceWrap = bufferResult; + queue.push(pcData); + byte[] pop = queue.pop(16, 0); + pcdataResult = pop; return new byte[0]; } if (plength % 16 != 0) { - ByteBuffer buffer = ByteBuffer.wrap(pcData); - int remainder = plength % 16; - int intercept = plength - remainder; - byte[] bytes = new byte[intercept]; - ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept); - byte[] bytes1 = new byte[remainder]; - byteBuffer.get(bytes1); - this.pcdataResult = bytes; - ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + remainder); - bufferResult.put(bytes1); - sourceWrap = bufferResult; + queue.push(pcData); + byte[] pop = queue.pop(16, 0); + pcdataResult = pop; } byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag); iPkgNum = 2; return bytes; } else { - if (16 > pcData.length + sourceWrap.array().length) { - ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); - bufferResult.put(sourceWrap.array()); - bufferResult.put(pcData); - sourceWrap = bufferResult; + if (16 > (pcData.length + queue.getLength())) { + queue.push(pcData); return new byte[0]; - } else if ((pcData.length + sourceWrap.array().length) % 16 == 0) { - ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); - bufferResult.put(sourceWrap.array()); - bufferResult.put(pcData); - byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); - byte[] bytes1 = api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag); - iPkgNum = 2; - return bytes1; - } else { - ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); - bufferResult.put(sourceWrap.array()); - bufferResult.put(pcData); - byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); - ByteBuffer buffer = ByteBuffer.wrap(bytes); - int remainder = bytes.length % 16; - int intercept = bytes.length - remainder; - byte[] pcdataByte = new byte[intercept]; - ByteBuffer byteBuffer = buffer.get(pcdataByte, 0, intercept); - byte[] bytes1 = new byte[remainder]; - byteBuffer.get(bytes1); - this.pcdataResult = pcdataByte; - ByteBuffer bufferResults = ByteBuffer.allocate(remainder); - bufferResults.put(bytes1); - sourceWrap = bufferResults; - - byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag); - iPkgNum = 2; - return bytes2; } + queue.push(pcData); + byte[] pop = queue.pop(16, 0); + byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag); + iPkgNum = 2; + return bytes2; } - } - else { - ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length ); - byte[] waitData = sourceWrap.array(); - nSourceWrap.put( waitData ); - nSourceWrap.put( pcData ); - sourceWrap = nSourceWrap; - - System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array())); - - int totalLen = sourceWrap.array().length; - if ( totalLen / 16 < 2){ + } else { + if (null==queue||null==pcData){ + throw new IllegalArgumentException(); + } + queue.push(pcData); +// int totalLen = queue.getLength() + pcData.length; + if ((queue.getLength()) / 16 < 2) { return new byte[0]; } - - - int com = totalLen % 16; - int sendBuffLen = totalLen - com -16; - byte[] sendBuff = new byte[sendBuffLen]; - - - - sourceWrap.position(0); - sourceWrap.get( sendBuff , 0, sendBuff.length ); - byte[] waitBuff = new byte[ totalLen - sendBuffLen ]; - sourceWrap.get( waitBuff , 0, waitBuff.length ); - - System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d", - pcData.length, - totalLen, - sendBuff.length, - sendBuff.length % 16, - iPkgNum) - ); - - System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) ); - - if( 0x00 == waitBuff[0] ) { - System.out.println("eee"); - } - sourceWrap = ByteBuffer.wrap( waitBuff ); - System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array())); - byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag); + byte[] pop = queue.pop(16, 16); + byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag); iPkgNum = 2; return bytes2; + +// ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length ); +// byte[] waitData = sourceWrap.array(); +// nSourceWrap.put( waitData ); +// nSourceWrap.put( pcData ); +// sourceWrap = nSourceWrap; +// +// System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array())); +// +// int totalLen = sourceWrap.array().length; +// if ( totalLen / 16 < 2){ +// return new byte[0]; +// } +// +// +// int com = totalLen % 16; +// int sendBuffLen = totalLen - com -16; +// byte[] sendBuff = new byte[sendBuffLen]; +// +// +// +// sourceWrap.position(0); +// sourceWrap.get( sendBuff , 0, sendBuff.length ); +// byte[] waitBuff = new byte[ totalLen - sendBuffLen ]; +// sourceWrap.get( waitBuff , 0, waitBuff.length ); +// +// System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d", +// pcData.length, +// totalLen, +// sendBuff.length, +// sendBuff.length % 16, +// iPkgNum) +// ); +// +// System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) ); +// +// if( 0x00 == waitBuff[0] ) { +// System.out.println("eee"); +// } +// sourceWrap = ByteBuffer.wrap( waitBuff ); +// System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array())); +// byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag); +// +// iPkgNum = 2; +// return bytes2; } } @@ -162,23 +140,22 @@ public class SydApiSM4 { this.iPkgNum = 3; } - if (iFlag == 0){ - byte[] d = sourceWrap.array(); - byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag); - sourceWrap = null; + + byte[] pop = queue.pop(); + byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag); return bytes; - } - byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), iPkgNum, iFlag); - sourceWrap = null; - return bytes; + +// byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, , iPkgNum, iFlag); +// sourceWrap = null; +// return bytes; } - public ByteBuffer bufferResult(int plength, byte[] pcData) { - ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); - bufferResult.put(sourceWrap); - bufferResult.put(pcData); - return bufferResult; - } +// public ByteBuffer bufferResult(int plength, byte[] pcData) { +// ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); +// bufferResult.put(sourceWrap); +// bufferResult.put(pcData); +// return bufferResult; +// } } diff --git a/src/main/java/com/sunyard/sge/database/SydApiSM423.java b/src/main/java/com/sunyard/sge/database/SydApiSM423.java new file mode 100644 index 0000000..cbdab29 --- /dev/null +++ b/src/main/java/com/sunyard/sge/database/SydApiSM423.java @@ -0,0 +1,186 @@ +package com.sunyard.sge.database; + +import com.sunyard.SydApiException; +import com.sunyard.proto.Util; + +import java.nio.ByteBuffer; + +public class SydApiSM423 { + + private SydApi api; + private int iKeyIndex; + private byte[] pcdataResult = new byte[0]; + private int iPkgNum = 1; + private int iFlag; + private boolean is_finished = false; + private byte[] dataResult = new byte[0]; + + ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]); + + + public SydApiSM423(SydApi api, int iKeyIndex, int endeFlag) { + this.api = api; + this.iFlag = endeFlag; + this.iKeyIndex = iKeyIndex; + } + + public byte[] update(byte[] pcData) { + this.pcdataResult = pcData; +// this.dataResult = pcData; + if (null == pcData || pcData.length == 0) { + return new byte[0]; + } + int plength = pcData.length; + if (is_finished) { + throw new SydApiException(0); + } + if (plength > 4064) { + throw new SydApiException(0); + } + if (iFlag == 1) { + if (0 == sourceWrap.array().length) { + //加密 + if (plength < 16) { + ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); + bufferResult.put(sourceWrap); + bufferResult.put(pcData); + sourceWrap = bufferResult; + return new byte[0]; + } + if (plength % 16 != 0) { + ByteBuffer buffer = ByteBuffer.wrap(pcData); + int remainder = plength % 16; + int intercept = plength - remainder; + byte[] bytes = new byte[intercept]; + ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept); + byte[] bytes1 = new byte[remainder]; + byteBuffer.get(bytes1); + this.pcdataResult = bytes; + ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + remainder); + bufferResult.put(bytes1); + sourceWrap = bufferResult; + } + byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag); + iPkgNum = 2; + return bytes; + + } else { + if (16 > pcData.length + sourceWrap.array().length) { + ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); + bufferResult.put(sourceWrap.array()); + bufferResult.put(pcData); + sourceWrap = bufferResult; + return new byte[0]; + } else if ((pcData.length + sourceWrap.array().length) % 16 == 0) { + ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); + bufferResult.put(sourceWrap.array()); + bufferResult.put(pcData); + byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); + byte[] bytes1 = api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag); + iPkgNum = 2; + return bytes1; + } else { + ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); + bufferResult.put(sourceWrap.array()); + bufferResult.put(pcData); + byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); + ByteBuffer buffer = ByteBuffer.wrap(bytes); + int remainder = bytes.length % 16; + int intercept = bytes.length - remainder; + byte[] pcdataByte = new byte[intercept]; + ByteBuffer byteBuffer = buffer.get(pcdataByte, 0, intercept); + byte[] bytes1 = new byte[remainder]; + byteBuffer.get(bytes1); + this.pcdataResult = pcdataByte; + ByteBuffer bufferResults = ByteBuffer.allocate(remainder); + bufferResults.put(bytes1); + sourceWrap = bufferResults; + + byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag); + iPkgNum = 2; + return bytes2; + } + } + } + else { + ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length ); + byte[] waitData = sourceWrap.array(); + nSourceWrap.put( waitData ); + nSourceWrap.put( pcData ); + sourceWrap = nSourceWrap; + + System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array())); + + int totalLen = sourceWrap.array().length; + if ( totalLen / 16 < 2){ + return new byte[0]; + } + + + int com = totalLen % 16; + int sendBuffLen = totalLen - com -16; + byte[] sendBuff = new byte[sendBuffLen]; + + + + sourceWrap.position(0); + sourceWrap.get( sendBuff , 0, sendBuff.length ); + byte[] waitBuff = new byte[ totalLen - sendBuffLen ]; + sourceWrap.get( waitBuff , 0, waitBuff.length ); + + System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d", + pcData.length, + totalLen, + sendBuff.length, + sendBuff.length % 16, + iPkgNum) + ); + + System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) ); + + if( 0x00 == waitBuff[0] ) { + System.out.println("eee"); + } + sourceWrap = ByteBuffer.wrap( waitBuff ); + System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array())); + byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag); + + iPkgNum = 2; + return bytes2; + } + + } + + public byte[] finish() { + if (is_finished) { + throw new SydApiException(0); + } + this.is_finished = true; + if (1 == this.iPkgNum) { + this.iPkgNum = 0; + } else if (2 == this.iPkgNum) { + this.iPkgNum = 3; + } + + if (iFlag == 0){ + byte[] d = sourceWrap.array(); + byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag); + sourceWrap = null; + return bytes; + } + byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), iPkgNum, iFlag); + sourceWrap = null; + return bytes; + } + + public ByteBuffer bufferResult(int plength, byte[] pcData) { + ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); + bufferResult.put(sourceWrap); + bufferResult.put(pcData); + return bufferResult; + } +} + + + + diff --git a/src/main/java/com/sunyard/sge/log/SydLogFactory.java b/src/main/java/com/sunyard/sge/log/SydLogFactory.java index f0173ec..b990861 100644 --- a/src/main/java/com/sunyard/sge/log/SydLogFactory.java +++ b/src/main/java/com/sunyard/sge/log/SydLogFactory.java @@ -118,4 +118,6 @@ public class SydLogFactory { public static SydLogger getLogger() { return new SydLogger(queue); } + + } diff --git a/src/test/java/test/LongDataTest.java b/src/test/java/test/LongDataTest.java index 98ad567..b89b3eb 100644 --- a/src/test/java/test/LongDataTest.java +++ b/src/test/java/test/LongDataTest.java @@ -13,13 +13,14 @@ import java.util.Random; public class LongDataTest { private SydApi api; + @Before - public void init(){ + public void init() { // System.setProperty("com.sunyard.sydapi4j.debug", "true"); SydApiBuilder builder = new SydApiBuilder( - new String[]{ "192.168.1.128" }, - new int[]{ 8889 }, + new String[]{"192.168.1.128"}, + new int[]{8889}, 10, 15 @@ -28,7 +29,7 @@ public class LongDataTest { } @Test - public void sm3_case1(){ + public void sm3_case1() { SydApiSM3 sm3 = api.initSM3(); sm3.update(null); String mac = sm3.digest(); @@ -36,7 +37,7 @@ public class LongDataTest { } @Test - public void sm3_case2(){ + public void sm3_case2() { SydApiSM3 sm3 = api.initSM3(); sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678")); String mac = sm3.digest(); @@ -44,7 +45,7 @@ public class LongDataTest { } @Test - public void sm3_case3(){ + public void sm3_case3() { SydApiSM3 sm3 = api.initSM3(); sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678")); sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678")); @@ -53,32 +54,32 @@ public class LongDataTest { } @Test - public void sm3_case4(){ + public void sm3_case4() { SydApiSM3 sm3 = api.initSM3(); - System.out.print( "org="); - for (int i =0 ; i < 100; i ++) { + System.out.print("org="); + for (int i = 0; i < 100; i++) { int lucky = new Random().nextInt(4065); byte[] data = new byte[lucky]; - new Random().nextBytes( data ); - System.out.print( Util.bytes2HexString(data) ); + new Random().nextBytes(data); + System.out.print(Util.bytes2HexString(data)); sm3.update(data); } String mac = sm3.digest(); System.out.println(); - System.out.println( mac ); + System.out.println(mac); } @Test - public void sm3_case5(){ + public void sm3_case5() { SydApiSM3 sm3 = api.initSM3(); String mac = sm3.digest(); Assert.assertEquals("1AB21D8355CFA17F8E61194831E81A8F22BEC8C728FEFB747ED035EB5082AA2B", mac); } @Test - public void sm4mac_case1(){ + public void sm4mac_case1() { SydSM4Mac sydApiMac = api.initSM4Mac(1); sydApiMac.update(null); String mac = (String) sydApiMac.disget().getData(); @@ -86,123 +87,103 @@ public class LongDataTest { } @Test - public void sm4mac_case2(){ + public void sm4mac_case2() { SydSM4Mac sydApiMac = api.initSM4Mac(1); String mac = (String) sydApiMac.disget().getData(); Assert.assertEquals("FF49F76AD3A71651FE1A8A9F0D2A689E", mac); } @Test - public void sm4mac_case3(){ + public void sm4mac_case3() { SydSM4Mac sydApiMac = api.initSM4Mac(1); - for (int i =0 ; i < 10; i ++) { - int lucky = new Random().nextInt(4065); - byte[] data = new byte[lucky]; - System.out.println("len="+lucky); + for (int i = 0; i < 10; i++) { +// int lucky = new Random().nextInt(4065); + byte[] data = new byte[66]; +// System.out.println("len=" + lucky); // new Random().nextBytes( data ); +// sydApiMac.setPcMac("405C2DCF4884240B7F2E4FF56B00639A"); + sydApiMac.setPcMac("72A7EFF0004CE7D2A1B7C2260726EC38"); sydApiMac.update(data); } - String mac = (String) sydApiMac.disget().getData(); - System.out.println( mac ); + boolean mac = (boolean) sydApiMac.disget().getData(); +// String mac1 = (String) sydApiMac.disget().getData(); + System.out.println(mac); } @Test - public void case5(){ - SydApiSM4 sydApiSM4 = api.initSM4(1,1); + public void case5() { + SydApiSM423 sydApiSM4 = api.initSM4(1, 1); byte[] enData = sydApiSM4.finish(); - System.out.println(Util.bytes2HexString( enData ) ); - Assert.assertArrayEquals( Util.hexString2Bytes("FF49F76AD3A71651FE1A8A9F0D2A689E"), enData ); + System.out.println(Util.bytes2HexString(enData)); + Assert.assertArrayEquals(Util.hexString2Bytes("FF49F76AD3A71651FE1A8A9F0D2A689E"), enData); } @Test - public void case6(){ - SydApiSM4 sydApiSM4 = api.initSM4(1,1); + public void case6() { + SydApiSM423 sydApiSM4 = api.initSM4(1, 1); - for (int i =0 ; i < 10; i ++) { + for (int i = 0; i < 10; i++) { int lucky = new Random().nextInt(4065); byte[] data = new byte[lucky]; - System.out.println("len="+lucky); + System.out.println("len=" + lucky); - new Random().nextBytes( data ); + new Random().nextBytes(data); sydApiSM4.update(data); } byte[] enData = sydApiSM4.finish(); - System.out.println(Util.bytes2HexString( enData ) ); - } - - @Test - public void case12(){ - for (int i =0 ; i < 1000; i ++) { - int lucky = new Random().nextInt(4065); - SydApiSM4 sydApiSM4 = api.initSM4( 1,1); -// byte[] inData = new byte[lucky]; -// byte[] update = sydApiSM4.update(new byte[89]); -// byte[] enData = sydApiSM4.finish(); -// ByteBuffer buffer = ByteBuffer.allocate(inData.length+enData.length); -// buffer.put(update); -// buffer.put(enData); - - SydApiSM4 sydApiSM42 = api.initSM4(1,0); - sydApiSM42.update(new byte[80]); - byte[] deData = sydApiSM42.finish(); - -// Assert.assertArrayEquals( deData , inData); - } + System.out.println(Util.bytes2HexString(enData)); } - - @Test - public void sm4(){ + public void sm4() { - int max = 1; + int max = 1000; int size = 4065; - SydApiSM4 en = api.initSM4( 1,1); - SydApiSM4 de = api.initSM4(1,0); + SydApiSM423 en = api.initSM4(1, 1); + SydApiSM423 de = api.initSM4(1, 0); - ByteBuffer inData = ByteBuffer.allocate( max * size ); - ByteBuffer enData = ByteBuffer.allocate( max * size + 16); - ByteBuffer deData = ByteBuffer.allocate( max * size ); + ByteBuffer inData = ByteBuffer.allocate(max * size); + ByteBuffer enData = ByteBuffer.allocate(max * size + 16); + ByteBuffer deData = ByteBuffer.allocate(max * size); - - for (int i =0 ; i < max; i ++) { + for (int i = 0; i < max; i++) { int lucky = new Random().nextInt(size); byte[] data = new byte[lucky]; - inData.put( data ); - System.out.println( "in=" + data.length ); - enData.put( en.update(data) ); + inData.put(data); + System.out.println("in=" + data.length); + enData.put(en.update(data)); } byte[] v = en.finish(); - System.out.println("finish=" + v.length ); - enData.put( v ); + System.out.println("finish=" + v.length); + enData.put(v); - System.out.println("原文=" + inData.position() ); - System.out.println("密文=" + enData.position() ); + System.out.println("原文=" + inData.position()); + System.out.println("密文=" + enData.position()); - if ( 1 == max ) { + if (1 == max) { - byte[] in = new byte[ inData.position() ]; - System.arraycopy( inData.array(), 0, in, 0, in.length ); + byte[] in = new byte[inData.position()]; + System.arraycopy(inData.array(), 0, in, 0, in.length); - byte[] bytes = api.SYD_SM4_ShortData(1, in , Consts.ECB_ENC); - System.out.println("对比 =" + Util.bytes2HexString(bytes) ); + byte[] bytes = api.SYD_SM4_ShortData(1, in, Consts.ECB_ENC); + System.out.println("对比 =" + Util.bytes2HexString(bytes)); - byte[] look = new byte[ enData.position() ]; - System.arraycopy( enData.array(), 0, look, 0, look.length ); - System.out.println( Util.bytes2HexString( look ) ); + byte[] look = new byte[enData.position()]; + System.arraycopy(enData.array(), 0, look, 0, look.length); + System.out.println(Util.bytes2HexString(look)); - System.out.println("eq=" + Arrays.equals( look, bytes )); + System.out.println("eq=" + Arrays.equals(look, bytes)); - byte[] deBytes = api.SYD_SM4_ShortData(1, bytes , Consts.ECB_DEC ); - System.out.println("还原=" + Util.bytes2HexString(deBytes) ); + byte[] deBytes = api.SYD_SM4_ShortData(1, bytes, Consts.ECB_DEC); + System.out.println("还原=" + Util.bytes2HexString(deBytes)); } @@ -211,44 +192,27 @@ public class LongDataTest { byte[] data = new byte[16]; - enData.get( data ); - deData.put( de.update(data) ); - while (true){ + enData.get(data); + deData.put(de.update(data)); + while (true) { int lucky = new Random().nextInt(size); - System.out.println( "lucky=" + lucky ); + System.out.println("lucky=" + lucky); data = new byte[lucky]; - if ( enData.position() + lucky >= enData.limit() ) { + if (enData.position() + lucky >= enData.limit()) { // 到头了 data = new byte[enData.limit() - enData.position()]; - enData.get( data ); - deData.put( de.update(data) ); - deData.put( de.finish() ); + enData.get(data); + deData.put(de.update(data)); + deData.put(de.finish()); break; } else { - enData.get( data ); - deData.put( de.update(data) ); + enData.get(data); + deData.put(de.update(data)); } } - Assert.assertArrayEquals( deData.array() , inData.array()); + Assert.assertArrayEquals(deData.array(), inData.array()); } - - @Test - public void case11(){ - SydApiSM4 sydApiSM4 = api.initSM4(1,1); - - for (int i =0 ; i < 1000; i ++) { - int lucky = new Random().nextInt(4065); - byte[] data = new byte[lucky]; - System.out.println("len="+lucky); - - new Random().nextBytes( data ); - byte[] update = sydApiSM4.update(data); - - - byte[] enData = sydApiSM4.finish(); - System.out.println(Util.bytes2HexString( enData ) ); - }} }