增加init/update/digest/finish长数据操作
This commit is contained in:
parent
a5f38b4a8f
commit
6ff2df3857
@ -13,6 +13,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]);
|
||||
|
||||
|
||||
@ -35,13 +36,10 @@ public class SydApiSM4 {
|
||||
if (plength > 4064) {
|
||||
throw new SydApiException(0);
|
||||
}
|
||||
if (iFlag == 1) {
|
||||
if (0 == sourceWrap.array().length) {
|
||||
//加密
|
||||
if (plength < 16) {
|
||||
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
|
||||
if (iFlag == 0 && 1 == iPkgNum) {
|
||||
throw new SydApiException("解密的首包不能小于 16 字节", 0);
|
||||
}
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap);
|
||||
bufferResult.put(pcData);
|
||||
@ -60,6 +58,7 @@ public class SydApiSM4 {
|
||||
bufferResult.put(bytes1);
|
||||
sourceWrap = bufferResult;
|
||||
}
|
||||
|
||||
iPkgNum = 2;
|
||||
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
||||
|
||||
@ -97,6 +96,73 @@ public class SydApiSM4 {
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (0 == sourceWrap.array().length) {
|
||||
if (plength<16){
|
||||
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap);
|
||||
bufferResult.put(pcData);
|
||||
sourceWrap = bufferResult;
|
||||
}
|
||||
ByteBuffer buffer = ByteBuffer.wrap(pcData);
|
||||
int remainder = plength % 16;
|
||||
int intercept = plength - remainder - 16;
|
||||
byte[] bytes = new byte[intercept];
|
||||
ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept);
|
||||
byte[] bytes1 = new byte[remainder];
|
||||
byteBuffer.get(bytes1);
|
||||
byte[] bytes2 = new byte[16];
|
||||
ByteBuffer byte1 = buffer.get(bytes2,0,16);
|
||||
dataResult=bytes2;
|
||||
this.pcdataResult = bytes;
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + remainder);
|
||||
bufferResult.put(bytes1);
|
||||
sourceWrap = bufferResult;
|
||||
|
||||
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(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);
|
||||
return api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
|
||||
} 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 - 16;
|
||||
byte[] pcdataByte = new byte[intercept];
|
||||
ByteBuffer byteBuffer = buffer.get(pcdataByte, 0, intercept);
|
||||
byte[] bytes1 = new byte[remainder];
|
||||
byteBuffer.get(bytes1);
|
||||
byte[] bytes2 = new byte[16];
|
||||
ByteBuffer byteBuffer2 = buffer.get(bytes2, 0, 16);
|
||||
this.dataResult = bytes2;
|
||||
this.pcdataResult = pcdataByte;
|
||||
ByteBuffer bufferResults = ByteBuffer.allocate(remainder);
|
||||
bufferResults.put(bytes1);
|
||||
sourceWrap = bufferResults;
|
||||
iPkgNum = 2;
|
||||
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] finish() {
|
||||
if (is_finished) {
|
||||
@ -109,6 +175,15 @@ public class SydApiSM4 {
|
||||
this.iPkgNum = 3;
|
||||
}
|
||||
|
||||
|
||||
if (iFlag == 0){
|
||||
ByteBuffer buffer = ByteBuffer.allocate(dataResult.length+sourceWrap.array().length);
|
||||
buffer.put(dataResult);
|
||||
buffer.put(sourceWrap.array());
|
||||
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,buffer.array(), iPkgNum, iFlag);
|
||||
sourceWrap = null;
|
||||
return bytes;
|
||||
}
|
||||
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), iPkgNum, iFlag);
|
||||
sourceWrap = null;
|
||||
return bytes;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user