Merge remote-tracking branch 'origin/dev-1.0' into dev-1.0

# Conflicts:
#	src/test/java/test/LongDataTest.java
This commit is contained in:
cheney 2022-08-31 20:36:38 +08:00
commit 35a167bf57
4 changed files with 29 additions and 11 deletions

View File

@ -272,7 +272,7 @@ public interface SydApi {
);
public SydApiSM4 initSM4(SydApi sydApi, int iKeyIndex, int ende);
public SydApiSM4 initSM4( int iKeyIndex, int ende);
public SydApiSM3 initSM3();

View File

@ -768,7 +768,7 @@ public class SydApi4Database implements SydApi {
@Override
public SydApiSM4 initSM4(SydApi sydApi, int iKeyIndex, int ende) {
public SydApiSM4 initSM4(int iKeyIndex, int ende) {
return new SydApiSM4(this,iKeyIndex,ende);
}

View File

@ -21,10 +21,10 @@ public class SydApiSM4 {
this.iKeyIndex = iKeyIndex;
}
public void update(byte[] pcData) {
public byte[] update(byte[] pcData) {
this.pcdataResult = pcData;
if (null == pcData || pcData.length == 0) {
return;
return new byte[0];
}
int plength = pcData.length;
if (is_finished) {
@ -58,20 +58,22 @@ public class SydApiSM4 {
bufferResult.put(bytes1);
sourceWrap = bufferResult;
}
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2;
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
} else {
if (16 > pcData.length + sourceWrap.array().length) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
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(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
return api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
} else {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
@ -88,8 +90,8 @@ public class SydApiSM4 {
ByteBuffer bufferResults = ByteBuffer.allocate(remainder);
bufferResults.put(bytes1);
sourceWrap = bufferResults;
api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2;
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
}
}
}

View File

@ -99,7 +99,7 @@ public class LongDataTest {
int lucky = new Random().nextInt(4065);
byte[] data = new byte[lucky];
System.out.println("len="+lucky);
new Random().nextBytes( data );
// new Random().nextBytes( data );
sydApiMac.update(data);
}
@ -113,7 +113,7 @@ public class LongDataTest {
@Test
public void case5(){
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
SydApiSM4 sydApiSM4 = api.initSM4(1,1);
byte[] enData = sydApiSM4.finish();
System.out.println(Util.bytes2HexString( enData ) );
Assert.assertArrayEquals( Util.hexString2Bytes("FF49F76AD3A71651FE1A8A9F0D2A689E"), enData );
@ -121,7 +121,7 @@ public class LongDataTest {
@Test
public void case6(){
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
SydApiSM4 sydApiSM4 = api.initSM4(1,1);
for (int i =0 ; i < 10; i ++) {
int lucky = new Random().nextInt(4065);
@ -136,6 +136,22 @@ public class LongDataTest {
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[] bytes = new byte[lucky];
sydApiSM4.update(new byte[lucky]);
byte[] finish = sydApiSM4.finish();
SydApiSM4 sydApiSM42 = api.initSM4(1,0);
sydApiSM42.update(finish);
byte[] finish1 = sydApiSM42.finish();
System.out.println("原始数据:"+Arrays.toString(bytes)+",\n"+"解密后数据:"+Arrays.toString(finish1));
}
}
}