增加init/update/digest/finish长数据操作
This commit is contained in:
parent
4070a7dcad
commit
5879ca9ed7
@ -168,13 +168,6 @@ public interface SydApi {
|
||||
int iFlag
|
||||
);
|
||||
|
||||
byte[] SYD_SM4_LongData2(
|
||||
int iKeyIndex,
|
||||
byte[] pcData,
|
||||
int iPkgNum,
|
||||
int iFlag
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* 批量数据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
|
||||
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
|
||||
public byte[] SYD_SM4_LongData2(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||
SydApiSM4 sydApiSM4 = initSydApiSM4(1, pcData[0]);
|
||||
byte[] update = sydApiSM4.update(iKeyIndex, pcData, iPkgNum, iFlag);
|
||||
public SydSM4Mac initSydSM4Mac(SydApi sydApi, int ende, int key) {
|
||||
return new SydSM4Mac(sydApi);
|
||||
}
|
||||
|
||||
return null;
|
||||
public SydSM4Mac initSydSM4Mac(SydApi sydApi){
|
||||
|
||||
return new SydSM4Mac(sydApi);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
@ -11,41 +10,45 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SydApiSM3 {
|
||||
private SydApi4Database api;
|
||||
private boolean is_finished = false;
|
||||
private SydApi api;
|
||||
private int iPkgNum;
|
||||
private String pcProcData;
|
||||
|
||||
public SydApiSM3(SydApi4Database sydApi4Database) {
|
||||
this.api = sydApi4Database;
|
||||
public SydApiSM3(SydApi sydApi) {
|
||||
this.api = sydApi;
|
||||
}
|
||||
public void update(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
this.pcProcData = pcProcData;
|
||||
if (is_finished){
|
||||
|
||||
public void update(byte[] pcData) {
|
||||
if (is_finished) {
|
||||
throw new SydApiException(0);
|
||||
}
|
||||
if (null == pcData||0 == pcData.length) {
|
||||
if (null == pcData || 0 == pcData.length) {
|
||||
pcData = new byte[0];
|
||||
}
|
||||
if (pcData.length>4064){
|
||||
if (pcData.length > 4064) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (null==pcProcData){
|
||||
if (null == this.pcProcData) {
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(pcData, 1, null);
|
||||
}
|
||||
else {
|
||||
this.pcProcData = stringStringLongDataReturn.getProcData();
|
||||
} else {
|
||||
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) {
|
||||
throw new SydApiException(0);
|
||||
}
|
||||
this.is_finished = true;
|
||||
if (null == pcProcData){
|
||||
return api.SYD_SM3_Hash_LongData(new byte[0], 0, null);
|
||||
}
|
||||
else {
|
||||
return api.SYD_SM3_Hash_LongData(new byte[0], 3,pcProcData);
|
||||
if (null == pcProcData) {
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM3_Hash_LongData(new byte[0], 0, null);
|
||||
return stringStringLongDataReturn.getData();
|
||||
} else {
|
||||
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;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.EntityCache;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@ -9,81 +10,112 @@ import java.util.List;
|
||||
|
||||
public class SydApiSM4 {
|
||||
|
||||
private SydApi4Database api;
|
||||
// private phHandle
|
||||
byte[] pcData;
|
||||
int iDataLen;
|
||||
String[] pcOutData;
|
||||
//多余出的字节长度
|
||||
int piOutDataLen;
|
||||
int key;
|
||||
int endeFlag;
|
||||
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;
|
||||
private SydApi api;
|
||||
private int iKeyIndex;
|
||||
private byte[] pcdataResult;
|
||||
private int iPkgNum;
|
||||
private int iFlag;
|
||||
private int key;
|
||||
private int endeFlag;
|
||||
private boolean is_finished = false;
|
||||
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[]{9,9,9});
|
||||
|
||||
|
||||
|
||||
public SydApiSM4(SydApi4Database api, int key, int endeFlag) {
|
||||
public SydApiSM4(SydApi api, int key, int endeFlag) {
|
||||
this.api = api;
|
||||
this.key = key;
|
||||
this.endeFlag = endeFlag;
|
||||
}
|
||||
|
||||
public byte[] update(int iKeyIndex, byte[] pcData, int iPkgNum, int iFlag) {
|
||||
public void update(byte[] pcData) {
|
||||
int plength = pcData.length;
|
||||
byte[] pcDatas = pcData;
|
||||
if (piOutDataLen == 0) {
|
||||
if (iFlag == 1) {
|
||||
if (plength < 16) {
|
||||
int falg = piOutDataLen;
|
||||
piOutDataLen += plength;
|
||||
bytes = new byte[piOutDataLen];
|
||||
System.arraycopy(pcData,0,bytes,falg,pcData.length);
|
||||
throw new IllegalArgumentException();
|
||||
if (is_finished) {
|
||||
throw new SydApiException(0);
|
||||
}
|
||||
if (plength > 4064) {
|
||||
throw new SydApiException(0);
|
||||
}
|
||||
if (0 == plength) {
|
||||
return;
|
||||
}
|
||||
if (0 == sourceWrap.array().length) {
|
||||
//加密
|
||||
if (plength < 16) {
|
||||
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
|
||||
if (iFlag == 0 && 1 == iKeyIndex) {
|
||||
throw new SydApiException("解密的首包不能小于 16 字节", 0);
|
||||
}
|
||||
int index = plength % 16;
|
||||
int findex = plength - index;
|
||||
if (index != 0) {
|
||||
pcDatas = Arrays.copyOf(pcData, findex);
|
||||
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);
|
||||
return bytes;
|
||||
} else {
|
||||
|
||||
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
|
||||
bufferResult.put(sourceWrap);
|
||||
bufferResult.put(pcData);
|
||||
sourceWrap = bufferResult;
|
||||
}
|
||||
|
||||
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 {
|
||||
int combinationLength = piOutDataLen+ plength;
|
||||
if (iFlag==1){
|
||||
if (combinationLength<16){
|
||||
piOutDataLen = piOutDataLen+combinationLength;
|
||||
int falg = piOutDataLen;
|
||||
bytes = new byte[piOutDataLen];
|
||||
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcData, iPkgNum, iFlag);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
// public finish(){
|
||||
// api.SYD_SM4_LongData();
|
||||
// }
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,17 +1,104 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public class SydSM4Mac {
|
||||
private SydApi4Database api;
|
||||
import com.sunyard.SydApiException;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public void update(int iKeyIndex,
|
||||
byte[] pcData,
|
||||
int iPkgNum,
|
||||
String pcProcData) {
|
||||
|
||||
public SydSM4Mac update(byte[] pcData) {
|
||||
int length = pcData.length;
|
||||
this.pcdataResult = pcData;
|
||||
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