增加init/update/digest/finish长数据操作

This commit is contained in:
junj2.liang 2022-09-01 19:35:34 +08:00
parent c60d9421a7
commit 70c5b35e37
2 changed files with 109 additions and 32 deletions

View File

@ -1,6 +1,7 @@
package com.sunyard.sge.database;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import java.nio.ByteBuffer;
@ -102,16 +103,48 @@ public class SydApiSM4 {
}
}
else {
if (pcData.length/16<2){
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 length=(pcData.length/16)-1;
byte[] bytes = new byte[16];
dataResult = bytes;
byte[] bytes1 = new byte[pcData.length-16];
System.arraycopy(pcData,length*16,bytes,0,16);
System.arraycopy(pcData,0,bytes1,0,pcData.length-16);
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, bytes1, iPkgNum, iFlag);
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;
}
@ -129,9 +162,9 @@ public class SydApiSM4 {
this.iPkgNum = 3;
}
if (iFlag == 0){
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,dataResult, iPkgNum, iFlag);
byte[] d = sourceWrap.array();
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag);
sourceWrap = null;
return bytes;
}

View File

@ -7,6 +7,7 @@ import org.junit.Before;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Random;
public class LongDataTest {
@ -17,7 +18,7 @@ public class LongDataTest {
// System.setProperty("com.sunyard.sydapi4j.debug", "true");
SydApiBuilder builder = new SydApiBuilder(
new String[]{ "192.168.0.200" },
new String[]{ "192.168.1.128" },
new int[]{ 8889 },
10,
15
@ -138,39 +139,44 @@ public class LongDataTest {
for (int i =0 ; i < 1000; i ++) {
int lucky = new Random().nextInt(4065);
SydApiSM4 sydApiSM4 = api.initSM4( 1,1);
byte[] inData = new byte[lucky];
sydApiSM4.update(new byte[lucky]);
byte[] enData = sydApiSM4.finish();
// 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(enData);
sydApiSM42.update(new byte[80]);
byte[] deData = sydApiSM42.finish();
Assert.assertArrayEquals( deData , inData);
// Assert.assertArrayEquals( deData , inData);
}
}
@Test
public void sm4(){
int max = 1;
int size = 4065;
SydApiSM4 en = api.initSM4( 1,1);
SydApiSM4 de = api.initSM4(1,0);
ByteBuffer inData = ByteBuffer.allocate( max * 4065 );
ByteBuffer enData = ByteBuffer.allocate( max * 4065 + 16);
ByteBuffer deData = ByteBuffer.allocate( max * 4065 );
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 ++) {
int lucky = new Random().nextInt(4065);
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) );
System.out.println("原文 p=" + inData.position() );
System.out.println("密文 p=" + enData.position() );
}
byte[] v = en.finish();
System.out.println("finish=" + v.length );
@ -178,14 +184,38 @@ public class LongDataTest {
System.out.println("原文=" + inData.position() );
System.out.println("密文=" + enData.position() );
if ( 1 == max ) {
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[] 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 ));
byte[] deBytes = api.SYD_SM4_ShortData(1, bytes , Consts.ECB_DEC );
System.out.println("还原=" + Util.bytes2HexString(deBytes) );
}
enData.flip();
byte[] data = new byte[16];
enData.get( data );
deData.put( de.update(data) );
for (int i = 0 ; i < max; i ++) {
int lucky = new Random().nextInt(4065);
while (true){
int lucky = new Random().nextInt(size);
System.out.println( "lucky=" + lucky );
data = new byte[lucky];
if ( enData.position() + lucky >= enData.limit() ) {
@ -193,18 +223,32 @@ public class LongDataTest {
data = new byte[enData.limit() - enData.position()];
enData.get( data );
deData.put( de.update(data) );
deData.put( de.finish() );
break;
} else {
enData.get( data );
deData.put( de.update(data) );
}
}
if ( enData.position() < enData.limit() ){
// 没到头
data = new byte[enData.limit() - enData.position()];
deData.put( de.update(data) );
}
deData.put( de.finish() );
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 ) );
}}
}