增加init/update/digest/finish长数据操作
This commit is contained in:
parent
7844c61bd0
commit
86a784c1ba
@ -276,5 +276,5 @@ public interface SydApi {
|
|||||||
|
|
||||||
public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key);
|
public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key);
|
||||||
|
|
||||||
public SydSM4Mac initSydSM4Mac(SydApi sydApi,int iKeyIndex,int ende, int key);
|
public SydSM4Mac initSydSM4Mac(SydApi sydApi,int iKeyIndex,int ende, int key,String pcMac);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -778,8 +778,8 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SydSM4Mac initSydSM4Mac(SydApi sydApi, int iKeyIndex,int ende, int key) {
|
public SydSM4Mac initSydSM4Mac(SydApi sydApi, int iKeyIndex,int ende, int key,String pcMac) {
|
||||||
return new SydSM4Mac(sydApi,iKeyIndex,1,null);
|
return new SydSM4Mac(sydApi,iKeyIndex,1,null,pcMac);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SydSM4Mac initSydSM4Mac(SydApi sydApi){
|
public SydSM4Mac initSydSM4Mac(SydApi sydApi){
|
||||||
|
|||||||
@ -31,6 +31,7 @@ public class SydApiSM4 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update(byte[] pcData) {
|
public void update(byte[] pcData) {
|
||||||
|
this.pcdataResult = pcData;
|
||||||
if (null==pcData||pcData.length==0){
|
if (null==pcData||pcData.length==0){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,10 +16,11 @@ public class SydSM4Mac {
|
|||||||
private byte[] pcdataResult = new byte[0];
|
private byte[] pcdataResult = new byte[0];
|
||||||
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
||||||
|
|
||||||
public SydSM4Mac(SydApi api, int iKeyIndex, int iPkgNum, String pcProcData) {
|
public SydSM4Mac(SydApi api, int iKeyIndex, int iPkgNum, String pcProcData,String pcMac) {
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.iKeyIndex = iKeyIndex;
|
this.iKeyIndex = iKeyIndex;
|
||||||
this.pcProcData = pcProcData;
|
this.pcProcData = pcProcData;
|
||||||
|
this.pcMac=pcMac;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SydSM4Mac(SydApi api) {
|
public SydSM4Mac(SydApi api) {
|
||||||
@ -27,7 +28,7 @@ public class SydSM4Mac {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SydSM4Mac update(byte[] pcData) {
|
public SydSM4Mac update(byte[] pcData) {
|
||||||
if (null==pcData||0 == pcData.length) {
|
if (null == pcData || 0 == pcData.length) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
int length = pcData.length;
|
int length = pcData.length;
|
||||||
@ -38,34 +39,89 @@ public class SydSM4Mac {
|
|||||||
if (length > 4064) {
|
if (length > 4064) {
|
||||||
throw new SydApiException(0);
|
throw new SydApiException(0);
|
||||||
}
|
}
|
||||||
|
if (0 == sourceWrap.array().length) {
|
||||||
if (length < 16) {
|
if (length < 16) {
|
||||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
||||||
bufferResult.put(sourceWrap);
|
bufferResult.put(sourceWrap);
|
||||||
bufferResult.put(pcData);
|
bufferResult.put(pcData);
|
||||||
sourceWrap = bufferResult;
|
sourceWrap = bufferResult;
|
||||||
}
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||||
if (length % 16 != 0) {
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, 0, pcProcData);
|
||||||
ByteBuffer buffer = ByteBuffer.wrap(pcData);
|
pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
int remainder = length % 16;
|
} else {
|
||||||
int intercept = length - remainder;
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, 0, pcProcData, pcMac);
|
||||||
byte[] bytes = new byte[intercept];
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
||||||
ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept);
|
return this;
|
||||||
byte[] bytes1 = new byte[remainder];
|
}
|
||||||
byteBuffer.get(bytes1);
|
}
|
||||||
this.pcdataResult = bytes;
|
if (length % 16 != 0) {
|
||||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + remainder);
|
ByteBuffer buffer = ByteBuffer.wrap(pcData);
|
||||||
bufferResult.put(bytes1);
|
int remainder = length % 16;
|
||||||
sourceWrap = bufferResult;
|
int intercept = length - remainder;
|
||||||
}
|
byte[] bytes = new byte[intercept];
|
||||||
if (null == this.pcMac || "".equals(this.pcMac)) {
|
ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept);
|
||||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData);
|
byte[] bytes1 = new byte[remainder];
|
||||||
pcProcData = stringStringLongDataReturn.getProcData();
|
byteBuffer.get(bytes1);
|
||||||
iPkgNum = 2;
|
this.pcdataResult = bytes;
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + remainder);
|
||||||
|
bufferResult.put(bytes1);
|
||||||
|
sourceWrap = bufferResult;
|
||||||
|
}
|
||||||
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||||
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData);
|
||||||
|
pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
|
iPkgNum = 2;
|
||||||
|
} else {
|
||||||
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData, pcMac);
|
||||||
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
||||||
|
iPkgNum = 2;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData, pcMac);
|
if (16 > pcData.length + sourceWrap.array().length) {
|
||||||
pcProcData = stringBooleanLongDataReturn.getProcData();
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
||||||
iPkgNum = 2;
|
bufferResult.put(sourceWrap);
|
||||||
|
bufferResult.put(pcData);
|
||||||
|
sourceWrap = bufferResult;
|
||||||
|
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
||||||
|
bufferResult.put(sourceWrap);
|
||||||
|
bufferResult.put(pcData);
|
||||||
|
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
||||||
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||||
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, bytes, iPkgNum, pcProcData);
|
||||||
|
pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
|
iPkgNum = 2;
|
||||||
|
} else {
|
||||||
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, bytes, iPkgNum, pcProcData, pcMac);
|
||||||
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
||||||
|
iPkgNum = 2;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
||||||
|
bufferResult.put(sourceWrap);
|
||||||
|
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;
|
||||||
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||||
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData);
|
||||||
|
pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
|
iPkgNum = 2;
|
||||||
|
} else {
|
||||||
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData, pcMac);
|
||||||
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
||||||
|
iPkgNum = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -88,16 +144,17 @@ public class SydSM4Mac {
|
|||||||
return stringBooleanLongDataReturn;
|
return stringBooleanLongDataReturn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) {
|
public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) {
|
||||||
//重置 limit 和postion 值
|
//重置 limit 和postion 值
|
||||||
buffer.flip();
|
buffer.flip();
|
||||||
//获取buffer中有效大小
|
//获取buffer中有效大小
|
||||||
int len=buffer.limit() - buffer.position();
|
int len = buffer.limit() - buffer.position();
|
||||||
|
|
||||||
byte [] bytes=new byte[len];
|
byte[] bytes = new byte[len];
|
||||||
|
|
||||||
for (int i = 0; i < bytes.length; i++) {
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
bytes[i]=buffer.get();
|
bytes[i] = buffer.get();
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user