增加init/update/digest/finish长数据操作
This commit is contained in:
parent
4070a7dcad
commit
5879ca9ed7
@ -168,13 +168,6 @@ public interface SydApi {
|
|||||||
int iFlag
|
int iFlag
|
||||||
);
|
);
|
||||||
|
|
||||||
byte[] SYD_SM4_LongData2(
|
|
||||||
int iKeyIndex,
|
|
||||||
byte[] pcData,
|
|
||||||
int iPkgNum,
|
|
||||||
int iFlag
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量数据MAC校验接口,对批量数据进行SM4 MAC计算。
|
* 批量数据MAC校验接口,对批量数据进行SM4 MAC计算。
|
||||||
@ -279,9 +272,9 @@ public interface SydApi {
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
public SydApiSM4 initSydApiSM4(int ende, int key);
|
public SydApiSM4 initSydApiSM4(SydApi sydApi,int ende, int key);
|
||||||
|
|
||||||
public SydApiSM3 initSydApiSM3(int ende, int key);
|
public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key);
|
||||||
|
|
||||||
public SydSM4Mac initSydSM4Mac(int ende, int key);
|
public SydSM4Mac initSydSM4Mac(SydApi sydApi,int ende, int key);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -768,24 +768,22 @@ public class SydApi4Database implements SydApi {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SydApiSM4 initSydApiSM4(int ende, int key) {
|
public SydApiSM4 initSydApiSM4(SydApi sydApi,int ende, int key) {
|
||||||
|
|
||||||
return new SydApiSM4(new SydApi4Database(),ende,key);
|
return new SydApiSM4(sydApi,ende,key);
|
||||||
}
|
}
|
||||||
public SydApiSM3 initSydApiSM3(int ende, int key) {
|
public SydApiSM3 initSydApiSM3(SydApi sydApi,int ende, int key) {
|
||||||
|
|
||||||
return new SydApiSM3(new SydApi4Database());
|
return new SydApiSM3(sydApi);
|
||||||
}
|
}
|
||||||
public SydSM4Mac initSydSM4Mac(int ende, int key){
|
|
||||||
|
|
||||||
return new SydSM4Mac(new SydApi4Database());
|
|
||||||
}
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] SYD_SM4_LongData2(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
public SydSM4Mac initSydSM4Mac(SydApi sydApi, int ende, int key) {
|
||||||
SydApiSM4 sydApiSM4 = initSydApiSM4(1, pcData[0]);
|
return new SydSM4Mac(sydApi);
|
||||||
byte[] update = sydApiSM4.update(iKeyIndex, pcData, iPkgNum, iFlag);
|
}
|
||||||
|
|
||||||
return null;
|
public SydSM4Mac initSydSM4Mac(SydApi sydApi){
|
||||||
|
|
||||||
|
return new SydSM4Mac(sydApi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package com.sunyard.sge.database;
|
package com.sunyard.sge.database;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import com.sunyard.SydApiException;
|
import com.sunyard.SydApiException;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
@ -11,41 +10,45 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SydApiSM3 {
|
public class SydApiSM3 {
|
||||||
private SydApi4Database api;
|
|
||||||
private boolean is_finished = false;
|
private boolean is_finished = false;
|
||||||
|
private SydApi api;
|
||||||
|
private int iPkgNum;
|
||||||
private String pcProcData;
|
private String pcProcData;
|
||||||
|
|
||||||
public SydApiSM3(SydApi4Database sydApi4Database) {
|
public SydApiSM3(SydApi sydApi) {
|
||||||
this.api = sydApi4Database;
|
this.api = sydApi;
|
||||||
}
|
}
|
||||||
public void update(byte[] pcData, int iPkgNum, String pcProcData) {
|
|
||||||
this.pcProcData = pcProcData;
|
public void update(byte[] pcData) {
|
||||||
if (is_finished){
|
if (is_finished) {
|
||||||
throw new SydApiException(0);
|
throw new SydApiException(0);
|
||||||
}
|
}
|
||||||
if (null == pcData||0 == pcData.length) {
|
if (null == pcData || 0 == pcData.length) {
|
||||||
pcData = new byte[0];
|
pcData = new byte[0];
|
||||||
}
|
}
|
||||||
if (pcData.length>4064){
|
if (pcData.length > 4064) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
if (null==pcProcData){
|
if (null == this.pcProcData) {
|
||||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(pcData, 1, null);
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(pcData, 1, null);
|
||||||
}
|
this.pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
else {
|
} else {
|
||||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(pcData, 2, pcProcData);
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(pcData, 2, pcProcData);
|
||||||
|
this.pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public LongDataReturn<String, String> digest(){
|
|
||||||
|
public String digest() {
|
||||||
if (is_finished) {
|
if (is_finished) {
|
||||||
throw new SydApiException(0);
|
throw new SydApiException(0);
|
||||||
}
|
}
|
||||||
this.is_finished = true;
|
this.is_finished = true;
|
||||||
if (null == pcProcData){
|
if (null == pcProcData) {
|
||||||
return api.SYD_SM3_Hash_LongData(new byte[0], 0, null);
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(new byte[0], 0, null);
|
||||||
}
|
return stringStringLongDataReturn.getData();
|
||||||
else {
|
} else {
|
||||||
return api.SYD_SM3_Hash_LongData(new byte[0], 3,pcProcData);
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(new byte[0], 3, pcProcData);
|
||||||
|
return stringStringLongDataReturn.getData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.sunyard.sge.database;
|
package com.sunyard.sge.database;
|
||||||
|
|
||||||
|
import com.sunyard.SydApiException;
|
||||||
import com.sunyard.sge.EntityCache;
|
import com.sunyard.sge.EntityCache;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -9,81 +10,112 @@ import java.util.List;
|
|||||||
|
|
||||||
public class SydApiSM4 {
|
public class SydApiSM4 {
|
||||||
|
|
||||||
private SydApi4Database api;
|
private SydApi api;
|
||||||
// private phHandle
|
private int iKeyIndex;
|
||||||
byte[] pcData;
|
private byte[] pcdataResult;
|
||||||
int iDataLen;
|
private int iPkgNum;
|
||||||
String[] pcOutData;
|
private int iFlag;
|
||||||
//多余出的字节长度
|
private int key;
|
||||||
int piOutDataLen;
|
private int endeFlag;
|
||||||
int key;
|
private boolean is_finished = false;
|
||||||
int endeFlag;
|
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[]{9,9,9});
|
||||||
int ret = 0, i = 0;
|
|
||||||
int decloopnum = 0;
|
|
||||||
// int tmpdatalen = MAX_LIN * 8;
|
|
||||||
int alltmpdatalen = 0;
|
|
||||||
EntityCache entityCache = new EntityCache();
|
|
||||||
int k = 0;
|
|
||||||
byte[] bytes = new byte[k];
|
|
||||||
ByteBuffer byteBuffer;
|
|
||||||
|
|
||||||
|
|
||||||
|
public SydApiSM4(SydApi api, int key, int endeFlag) {
|
||||||
public SydApiSM4(SydApi4Database api, int key, int endeFlag) {
|
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.endeFlag = endeFlag;
|
this.endeFlag = endeFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] update(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
public void update(byte[] pcData) {
|
||||||
int plength = pcData.length;
|
int plength = pcData.length;
|
||||||
byte[] pcDatas = pcData;
|
if (is_finished) {
|
||||||
if (piOutDataLen == 0) {
|
throw new SydApiException(0);
|
||||||
if (iFlag == 1) {
|
}
|
||||||
|
if (plength > 4064) {
|
||||||
|
throw new SydApiException(0);
|
||||||
|
}
|
||||||
|
if (0 == plength) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (0 == sourceWrap.array().length) {
|
||||||
|
//加密
|
||||||
if (plength < 16) {
|
if (plength < 16) {
|
||||||
int falg = piOutDataLen;
|
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
|
||||||
piOutDataLen += plength;
|
if (iFlag == 0 && 1 == iKeyIndex) {
|
||||||
bytes = new byte[piOutDataLen];
|
throw new SydApiException("解密的首包不能小于 16 字节", 0);
|
||||||
System.arraycopy(pcData,0,bytes,falg,pcData.length);
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
}
|
||||||
int index = plength % 16;
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||||
int findex = plength - index;
|
bufferResult.put(sourceWrap);
|
||||||
if (index != 0) {
|
bufferResult.put(pcData);
|
||||||
pcDatas = Arrays.copyOf(pcData, findex);
|
sourceWrap = bufferResult;
|
||||||
int falg = piOutDataLen;
|
|
||||||
piOutDataLen += plength-findex;
|
|
||||||
bytes = new byte[piOutDataLen];
|
|
||||||
System.arraycopy(pcData,findex,bytes,falg,plength-findex);
|
|
||||||
}
|
}
|
||||||
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcDatas, iPkgNum, iFlag);
|
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, pcData, iPkgNum, iFlag);
|
||||||
|
iKeyIndex = 2;
|
||||||
|
} else {
|
||||||
|
if (16 > pcData.length + sourceWrap.array().length) {
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||||
|
bufferResult.put(sourceWrap);
|
||||||
|
bufferResult.put(pcData);
|
||||||
|
sourceWrap = bufferResult;
|
||||||
|
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||||
|
bufferResult.put(sourceWrap);
|
||||||
|
bufferResult.put(pcData);
|
||||||
|
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
||||||
|
api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||||
|
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;
|
||||||
|
ByteBuffer byteBuffer = buffer.get(bytes, 0, intercept);
|
||||||
|
byte[] bytes1 = new byte[remainder];
|
||||||
|
byteBuffer.get(bytes1);
|
||||||
|
this.pcdataResult = bytes;
|
||||||
|
ByteBuffer bufferResults = ByteBuffer.allocate(sourceWrap.array().length + remainder);
|
||||||
|
bufferResult.put(bytes1);
|
||||||
|
sourceWrap = bufferResults;
|
||||||
|
api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
||||||
|
iKeyIndex = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] finish() {
|
||||||
|
if (is_finished) {
|
||||||
|
throw new SydApiException(0);
|
||||||
|
}
|
||||||
|
this.is_finished = true;
|
||||||
|
if (1 == this.iKeyIndex) {
|
||||||
|
this.iKeyIndex = 0;
|
||||||
|
} else if (2 == this.iKeyIndex) {
|
||||||
|
this.iKeyIndex = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
|
||||||
|
sourceWrap = null;
|
||||||
return bytes;
|
return bytes;
|
||||||
} else {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
int combinationLength = piOutDataLen+ plength;
|
|
||||||
if (iFlag==1){
|
|
||||||
if (combinationLength<16){
|
|
||||||
piOutDataLen = piOutDataLen+combinationLength;
|
|
||||||
int falg = piOutDataLen;
|
|
||||||
bytes = new byte[piOutDataLen];
|
|
||||||
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcData, iPkgNum, iFlag);
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// public finish(){
|
|
||||||
// api.SYD_SM4_LongData();
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,17 +1,104 @@
|
|||||||
package com.sunyard.sge.database;
|
package com.sunyard.sge.database;
|
||||||
|
|
||||||
public class SydSM4Mac {
|
import com.sunyard.SydApiException;
|
||||||
private SydApi4Database api;
|
|
||||||
|
|
||||||
public SydSM4Mac(SydApi4Database api) {
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class SydSM4Mac {
|
||||||
|
private SydApi api;
|
||||||
|
private byte[] last_in_data;
|
||||||
|
private int iKeyIndex = 1;
|
||||||
|
private int iPkgNum;
|
||||||
|
private String pcProcData;
|
||||||
|
private boolean is_finished;
|
||||||
|
private String pcMac;
|
||||||
|
private byte[] pcdataResult;
|
||||||
|
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
||||||
|
|
||||||
|
public SydSM4Mac(SydApi api, int iKeyIndex, int iPkgNum, String pcProcData) {
|
||||||
|
this.api = api;
|
||||||
|
this.iKeyIndex = iKeyIndex;
|
||||||
|
this.iPkgNum = iPkgNum;
|
||||||
|
this.pcProcData = pcProcData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SydSM4Mac(SydApi api) {
|
||||||
this.api = api;
|
this.api = api;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(int iKeyIndex,
|
public SydSM4Mac update(byte[] pcData) {
|
||||||
byte[] pcData,
|
int length = pcData.length;
|
||||||
int iPkgNum,
|
this.pcdataResult = pcData;
|
||||||
String pcProcData) {
|
if (is_finished) {
|
||||||
|
throw new SydApiException(0);
|
||||||
|
}
|
||||||
|
if (length > 4064) {
|
||||||
|
throw new SydApiException(0);
|
||||||
|
}
|
||||||
|
if (0 == length) {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
if (length < 16) {
|
||||||
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
||||||
|
bufferResult.put(sourceWrap);
|
||||||
|
bufferResult.put(pcData);
|
||||||
|
sourceWrap = bufferResult;
|
||||||
|
}
|
||||||
|
if (length % 16 != 0) {
|
||||||
|
ByteBuffer buffer = ByteBuffer.wrap(pcData);
|
||||||
|
int remainder = length % 16;
|
||||||
|
int intercept = length - 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;
|
||||||
|
}
|
||||||
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||||
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData);
|
||||||
|
pcProcData = stringStringLongDataReturn.getProcData();
|
||||||
|
iKeyIndex = 2;
|
||||||
|
} else {
|
||||||
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iKeyIndex, pcProcData, pcMac);
|
||||||
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
||||||
|
iKeyIndex = 2;
|
||||||
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LongDataReturn disget() {
|
||||||
|
if (is_finished) {
|
||||||
|
throw new SydApiException(0);
|
||||||
|
}
|
||||||
|
this.is_finished = true;
|
||||||
|
if (1 == this.iKeyIndex) {
|
||||||
|
this.iKeyIndex = 2;
|
||||||
|
} else if (2 == this.iKeyIndex) {
|
||||||
|
this.iKeyIndex = 3;
|
||||||
|
}
|
||||||
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||||
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iKeyIndex, pcProcData);
|
||||||
|
return stringStringLongDataReturn;
|
||||||
|
} else {
|
||||||
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iKeyIndex, pcProcData, pcMac);
|
||||||
|
return stringBooleanLongDataReturn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) {
|
||||||
|
//重置 limit 和postion 值
|
||||||
|
buffer.flip();
|
||||||
|
//获取buffer中有效大小
|
||||||
|
int len=buffer.limit() - buffer.position();
|
||||||
|
|
||||||
|
byte [] bytes=new byte[len];
|
||||||
|
|
||||||
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
|
bytes[i]=buffer.get();
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user