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

This commit is contained in:
junj2.liang 2022-08-31 10:40:48 +08:00
parent 5879ca9ed7
commit 09aabbf158
4 changed files with 45 additions and 36 deletions

View File

@ -272,9 +272,9 @@ public interface SydApi {
); );
public SydApiSM4 initSydApiSM4(SydApi sydApi,int ende, int key); public SydApiSM4 initSydApiSM4(SydApi sydApi,int iFlag,int ende, int key);
public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key); public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key);
public SydSM4Mac initSydSM4Mac(SydApi sydApi,int ende, int key); public SydSM4Mac initSydSM4Mac(SydApi sydApi,int iKeyIndex,int ende, int key);
} }

View File

@ -383,7 +383,7 @@ public class SydApi4Database implements SydApi {
if (LogFactory.iLogLevel == 3) { if (LogFactory.iLogLevel == 3) {
log.debug("SYD_SM4_LongData方法接收参数iKeyIndex{},pcData{},iPkgNum{},iFlag{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag); log.debug("SYD_SM4_LongData方法接收参数iKeyIndex{},pcData{},iPkgNum{},iFlag{}", iKeyIndex, Arrays.toString(pcData), iPkgNum, iFlag);
} }
if (iFlag == Consts.ECB_DEC && null == pcData || pcData.length == 0) { if (iFlag == Consts.ECB_DEC && (null == pcData || pcData.length == 0)) {
throw new SydApiException(19); throw new SydApiException(19);
} }
if (null == this.hsms || this.hsms.length < 1) { if (null == this.hsms || this.hsms.length < 1) {
@ -768,9 +768,9 @@ public class SydApi4Database implements SydApi {
@Override @Override
public SydApiSM4 initSydApiSM4(SydApi sydApi,int ende, int key) { public SydApiSM4 initSydApiSM4(SydApi sydApi,int iFlag,int ende, int key) {
return new SydApiSM4(sydApi,ende,key); return new SydApiSM4(sydApi,iFlag,ende,key);
} }
public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key) { public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key) {
@ -778,8 +778,8 @@ public class SydApi4Database implements SydApi {
} }
@Override @Override
public SydSM4Mac initSydSM4Mac(SydApi sydApi, int ende, int key) { public SydSM4Mac initSydSM4Mac(SydApi sydApi, int iKeyIndex,int ende, int key) {
return new SydSM4Mac(sydApi); return new SydSM4Mac(sydApi,iKeyIndex,1,null);
} }
public SydSM4Mac initSydSM4Mac(SydApi sydApi){ public SydSM4Mac initSydSM4Mac(SydApi sydApi){

View File

@ -11,23 +11,28 @@ import java.util.List;
public class SydApiSM4 { public class SydApiSM4 {
private SydApi api; private SydApi api;
private int iKeyIndex; private int iKeyIndex=1;
private byte[] pcdataResult; private byte[] pcdataResult = new byte[0];
private int iPkgNum; private int iPkgNum = 1;
private int iFlag; private int iFlag;
private int key; private int key;
private int endeFlag; private int endeFlag;
private boolean is_finished = false; private boolean is_finished = false;
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[]{9,9,9}); ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
public SydApiSM4(SydApi api, int key, int endeFlag) {
public SydApiSM4(SydApi api, int key, int endeFlag,int iFlag) {
this.api = api; this.api = api;
this.key = key; this.key = key;
this.endeFlag = endeFlag; this.endeFlag = endeFlag;
this.iFlag = iFlag;
} }
public void update(byte[] pcData) { public void update(byte[] pcData) {
if (null==pcData||pcData.length==0){
return;
}
int plength = pcData.length; int plength = pcData.length;
if (is_finished) { if (is_finished) {
throw new SydApiException(0); throw new SydApiException(0);
@ -35,9 +40,6 @@ public class SydApiSM4 {
if (plength > 4064) { if (plength > 4064) {
throw new SydApiException(0); throw new SydApiException(0);
} }
if (0 == plength) {
return;
}
if (0 == sourceWrap.array().length) { if (0 == sourceWrap.array().length) {
//加密 //加密
if (plength < 16) { if (plength < 16) {
@ -79,6 +81,7 @@ public class SydApiSM4 {
api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag); api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
} }
else { else {
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);
@ -86,15 +89,16 @@ public class SydApiSM4 {
ByteBuffer buffer = ByteBuffer.wrap(bytes); ByteBuffer buffer = ByteBuffer.wrap(bytes);
int remainder = bytes.length % 16; int remainder = bytes.length % 16;
int intercept = bytes.length - remainder; int intercept = bytes.length - remainder;
ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept); byte[] pcdataByte = new byte[intercept];
ByteBuffer byteBuffer = buffer.get(pcdataByte, 0, intercept);
byte[] bytes1 = new byte[remainder]; byte[] bytes1 = new byte[remainder];
byteBuffer.get(bytes1); byteBuffer.get(bytes1);
this.pcdataResult = bytes; this.pcdataResult = pcdataByte;
ByteBuffer bufferResults = ByteBuffer.allocate(sourceWrap.array().length + remainder); ByteBuffer bufferResults = ByteBuffer.allocate(remainder);
bufferResult.put(bytes1); bufferResults.put(bytes1);
sourceWrap = bufferResults; sourceWrap = bufferResults;
api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag); api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iKeyIndex = 2; iPkgNum = 2;
} }
} }
} }
@ -104,16 +108,22 @@ public class SydApiSM4 {
throw new SydApiException(0); throw new SydApiException(0);
} }
this.is_finished = true; this.is_finished = true;
if (1 == this.iKeyIndex) { if (1 == this.iPkgNum) {
this.iKeyIndex = 0; this.iPkgNum = 0;
} else if (2 == this.iKeyIndex) { } else if (2 == this.iPkgNum) {
this.iKeyIndex = 3; this.iPkgNum = 3;
} }
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag); byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
sourceWrap = null; sourceWrap = null;
return bytes; 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

@ -8,18 +8,17 @@ import java.nio.charset.StandardCharsets;
public class SydSM4Mac { public class SydSM4Mac {
private SydApi api; private SydApi api;
private byte[] last_in_data; private byte[] last_in_data;
private int iKeyIndex = 1; private int iKeyIndex;
private int iPkgNum; private int iPkgNum = 1;
private String pcProcData; private String pcProcData;
private boolean is_finished; private boolean is_finished;
private String pcMac; private String pcMac;
private byte[] pcdataResult; 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) {
this.api = api; this.api = api;
this.iKeyIndex = iKeyIndex; this.iKeyIndex = iKeyIndex;
this.iPkgNum = iPkgNum;
this.pcProcData = pcProcData; this.pcProcData = pcProcData;
} }
@ -61,11 +60,11 @@ public class SydSM4Mac {
if (null == this.pcMac || "".equals(this.pcMac)) { if (null == this.pcMac || "".equals(this.pcMac)) {
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData); LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData);
pcProcData = stringStringLongDataReturn.getProcData(); pcProcData = stringStringLongDataReturn.getProcData();
iKeyIndex = 2; iPkgNum = 2;
} else { } else {
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData, pcMac); LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData, pcMac);
pcProcData = stringBooleanLongDataReturn.getProcData(); pcProcData = stringBooleanLongDataReturn.getProcData();
iKeyIndex = 2; iPkgNum = 2;
} }
return this; return this;
} }
@ -75,16 +74,16 @@ public class SydSM4Mac {
throw new SydApiException(0); throw new SydApiException(0);
} }
this.is_finished = true; this.is_finished = true;
if (1 == this.iKeyIndex) { if (1 == this.iPkgNum) {
this.iKeyIndex = 2; this.iPkgNum = 0;
} else if (2 == this.iKeyIndex) { } else if (2 == this.iPkgNum) {
this.iKeyIndex = 3; this.iPkgNum = 3;
} }
if (null == this.pcMac || "".equals(this.pcMac)) { if (null == this.pcMac || "".equals(this.pcMac)) {
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iKeyIndex, pcProcData); LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iPkgNum, pcProcData);
return stringStringLongDataReturn; return stringStringLongDataReturn;
} else { } else {
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iKeyIndex, pcProcData, pcMac); LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iPkgNum, pcProcData, pcMac);
return stringBooleanLongDataReturn; return stringBooleanLongDataReturn;
} }
} }