增加init/update/digest/finish长数据操作
This commit is contained in:
parent
849c380dbe
commit
030849f754
@ -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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
public class LongDataTest {
|
||||
@ -77,7 +78,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);
|
||||
}
|
||||
|
||||
@ -111,6 +112,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(api, 1,1);
|
||||
byte[] bytes = new byte[lucky];
|
||||
sydApiSM4.update(new byte[lucky]);
|
||||
byte[] finish = sydApiSM4.finish();
|
||||
SydApiSM4 sydApiSM42 = api.initSM4(api, 1,0);
|
||||
sydApiSM42.update(finish);
|
||||
byte[] finish1 = sydApiSM42.finish();
|
||||
System.out.println("原始数据:"+Arrays.toString(bytes)+",\n"+"解密后数据:"+Arrays.toString(finish1));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user