接口文档更新

This commit is contained in:
junj2.liang 2022-09-05 10:22:27 +08:00
parent 43a75755ff
commit f77b5daa93
6 changed files with 356 additions and 229 deletions

View File

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

View File

@ -3,14 +3,12 @@ package com.sunyard.sge.database;
import com.sunyard.SydApiException; import com.sunyard.SydApiException;
import com.sunyard.proto.Util; import com.sunyard.proto.Util;
import com.sunyard.sge.database.pool.HsmLinkInfo; import com.sunyard.sge.database.pool.HsmLinkInfo;
import com.sunyard.sge.database.pool.SydApiPool;
import com.sunyard.sge.log.LogFactory; import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.pool.ObjectInPool; import com.sunyard.sge.pool.ObjectInPool;
import com.sunyard.util.SYMUtil; import com.sunyard.util.SYMUtil;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import racal.sunyard.main.SydApi4j; import racal.sunyard.main.SydApi4j;
import javax.annotation.CheckForNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -132,7 +130,7 @@ public class SydApi4Database implements SydApi {
if (null == this.hsms || this.hsms.length < 1) { if (null == this.hsms || this.hsms.length < 1) {
throw new SydApiException(-1); throw new SydApiException(-1);
} }
SydApiException le = new SydApiException(0); SydApiException le = new SydApiException(-1);
updateLastUseTime(); updateLastUseTime();
for (SydApi4j api : this.hsms) { 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(); updateLastUseTime();
for (SydApi4j api : this.hsms) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
@ -254,7 +252,7 @@ public class SydApi4Database implements SydApi {
updateLastUseTime(); updateLastUseTime();
String keyIndex = String.format("%08d", iKeyIndex); String keyIndex = String.format("%08d", iKeyIndex);
SydApiException le = new SydApiException(0); SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
continue; continue;
@ -335,7 +333,7 @@ public class SydApi4Database implements SydApi {
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[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) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
continue; continue;
@ -394,7 +392,7 @@ public class SydApi4Database implements SydApi {
updateLastUseTime(); updateLastUseTime();
String keyIndex = String.format("%08d", iKeyIndex); String keyIndex = String.format("%08d", iKeyIndex);
SydApiException le = new SydApiException(0); SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
continue; continue;
@ -477,7 +475,7 @@ public class SydApi4Database implements SydApi {
for (int i = 0; i < iKeyIndex.length; i++) { for (int i = 0; i < iKeyIndex.length; i++) {
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[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) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
continue; continue;
@ -545,7 +543,7 @@ public class SydApi4Database implements SydApi {
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[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) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
continue; continue;
@ -615,7 +613,7 @@ public class SydApi4Database implements SydApi {
if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) { if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) {
iv = pcProcData.getBytes(); iv = pcProcData.getBytes();
} }
SydApiException le = new SydApiException(0); SydApiException le = new SydApiException(-1);
updateLastUseTime(); updateLastUseTime();
for (SydApi4j api : this.hsms) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
@ -714,7 +712,7 @@ public class SydApi4Database implements SydApi {
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex)); String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值{}", keyIndex); log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值{}", keyIndex);
SydApiException le = new SydApiException(0); SydApiException le = new SydApiException(-1);
updateLastUseTime(); updateLastUseTime();
for (SydApi4j api : this.hsms) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
@ -775,9 +773,9 @@ public class SydApi4Database implements SydApi {
@Override @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() { public SydApiSM3 initSM3() {

View File

@ -2,6 +2,7 @@ package com.sunyard.sge.database;
import com.sunyard.SydApiException; import com.sunyard.SydApiException;
import com.sunyard.proto.Util; import com.sunyard.proto.Util;
import com.sunyard.sge.bytes.BytesQueue;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -14,8 +15,7 @@ public class SydApiSM4 {
private int iFlag; private int iFlag;
private boolean is_finished = false; private boolean is_finished = false;
private byte[] dataResult = new byte[0]; private byte[] dataResult = new byte[0];
BytesQueue queue = new BytesQueue();
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) { public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
@ -38,115 +38,93 @@ public class SydApiSM4 {
throw new SydApiException(0); throw new SydApiException(0);
} }
if (iFlag == 1) { if (iFlag == 1) {
if (0 == sourceWrap.array().length) { if (0 == queue.getLength()) {
//加密 //加密
if (plength < 16) { if (plength < 16) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); queue.push(pcData);
bufferResult.put(sourceWrap); byte[] pop = queue.pop(16, 0);
bufferResult.put(pcData); pcdataResult = pop;
sourceWrap = bufferResult;
return new byte[0]; return new byte[0];
} }
if (plength % 16 != 0) { if (plength % 16 != 0) {
ByteBuffer buffer = ByteBuffer.wrap(pcData); queue.push(pcData);
int remainder = plength % 16; byte[] pop = queue.pop(16, 0);
int intercept = plength - remainder; pcdataResult = pop;
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); byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2; iPkgNum = 2;
return bytes; return bytes;
} else { } else {
if (16 > pcData.length + sourceWrap.array().length) { if (16 > (pcData.length + queue.getLength())) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); queue.push(pcData);
bufferResult.put(sourceWrap.array());
bufferResult.put(pcData);
sourceWrap = bufferResult;
return new byte[0]; return new byte[0];
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) { }
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); queue.push(pcData);
bufferResult.put(sourceWrap.array()); byte[] pop = queue.pop(16, 0);
bufferResult.put(pcData); byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
byte[] bytes1 = api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
iPkgNum = 2; iPkgNum = 2;
return bytes1; return bytes2;
}
} else { } else {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); if (null==queue||null==pcData){
bufferResult.put(sourceWrap.array()); throw new IllegalArgumentException();
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);
} // int totalLen = queue.getLength() + pcData.length;
else { if ((queue.getLength()) / 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]; return new byte[0];
} }
byte[] pop = queue.pop(16, 16);
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, 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; iPkgNum = 2;
return bytes2; 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; this.iPkgNum = 3;
} }
if (iFlag == 0){
byte[] d = sourceWrap.array(); byte[] pop = queue.pop();
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag); byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
sourceWrap = null;
return bytes;
}
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), iPkgNum, iFlag);
sourceWrap = null;
return bytes; return bytes;
// byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, , iPkgNum, iFlag);
// sourceWrap = null;
// return bytes;
} }
public ByteBuffer bufferResult(int plength, byte[] pcData) { // public ByteBuffer bufferResult(int plength, byte[] pcData) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); // ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap); // bufferResult.put(sourceWrap);
bufferResult.put(pcData); // bufferResult.put(pcData);
return bufferResult; // return bufferResult;
} // }
} }

View File

@ -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;
}
}

View File

@ -118,4 +118,6 @@ public class SydLogFactory {
public static SydLogger getLogger() { public static SydLogger getLogger() {
return new SydLogger(queue); return new SydLogger(queue);
} }
} }

View File

@ -13,6 +13,7 @@ import java.util.Random;
public class LongDataTest { public class LongDataTest {
private SydApi api; private SydApi api;
@Before @Before
public void init() { public void init() {
// System.setProperty("com.sunyard.sydapi4j.debug", "true"); // System.setProperty("com.sunyard.sydapi4j.debug", "true");
@ -97,21 +98,24 @@ public class LongDataTest {
SydSM4Mac sydApiMac = api.initSM4Mac(1); SydSM4Mac sydApiMac = api.initSM4Mac(1);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
int lucky = new Random().nextInt(4065); // int lucky = new Random().nextInt(4065);
byte[] data = new byte[lucky]; byte[] data = new byte[66];
System.out.println("len="+lucky); // System.out.println("len=" + lucky);
// new Random().nextBytes( data ); // new Random().nextBytes( data );
// sydApiMac.setPcMac("405C2DCF4884240B7F2E4FF56B00639A");
sydApiMac.setPcMac("72A7EFF0004CE7D2A1B7C2260726EC38");
sydApiMac.update(data); sydApiMac.update(data);
} }
String mac = (String) sydApiMac.disget().getData(); boolean mac = (boolean) sydApiMac.disget().getData();
// String mac1 = (String) sydApiMac.disget().getData();
System.out.println(mac); System.out.println(mac);
} }
@Test @Test
public void case5() { public void case5() {
SydApiSM4 sydApiSM4 = api.initSM4(1,1); SydApiSM423 sydApiSM4 = api.initSM4(1, 1);
byte[] enData = sydApiSM4.finish(); byte[] enData = sydApiSM4.finish();
System.out.println(Util.bytes2HexString(enData)); System.out.println(Util.bytes2HexString(enData));
Assert.assertArrayEquals(Util.hexString2Bytes("FF49F76AD3A71651FE1A8A9F0D2A689E"), enData); Assert.assertArrayEquals(Util.hexString2Bytes("FF49F76AD3A71651FE1A8A9F0D2A689E"), enData);
@ -119,7 +123,7 @@ public class LongDataTest {
@Test @Test
public void case6() { public void case6() {
SydApiSM4 sydApiSM4 = api.initSM4(1,1); 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); int lucky = new Random().nextInt(4065);
@ -134,43 +138,20 @@ public class LongDataTest {
System.out.println(Util.bytes2HexString(enData)); 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);
}
}
@Test @Test
public void sm4() { public void sm4() {
int max = 1; int max = 1000;
int size = 4065; int size = 4065;
SydApiSM4 en = api.initSM4( 1,1); SydApiSM423 en = api.initSM4(1, 1);
SydApiSM4 de = api.initSM4(1,0); SydApiSM423 de = api.initSM4(1, 0);
ByteBuffer inData = ByteBuffer.allocate(max * size); ByteBuffer inData = ByteBuffer.allocate(max * size);
ByteBuffer enData = ByteBuffer.allocate(max * size + 16); ByteBuffer enData = ByteBuffer.allocate(max * size + 16);
ByteBuffer deData = ByteBuffer.allocate(max * size); 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); int lucky = new Random().nextInt(size);
byte[] data = new byte[lucky]; byte[] data = new byte[lucky];
@ -234,21 +215,4 @@ public class LongDataTest {
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 ) );
}}
} }