169 lines
7.4 KiB
Java
169 lines
7.4 KiB
Java
package com.sunyard.sge.database;
|
|
|
|
import com.sunyard.SydApiException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
|
|
public class SydSM4Mac {
|
|
private SydApi api;
|
|
private byte[] last_in_data;
|
|
private int iKeyIndex;
|
|
private int iPkgNum = 1;
|
|
private String pcProcData;
|
|
private boolean is_finished;
|
|
private String pcMac;
|
|
private byte[] pcdataResult = new byte[0];
|
|
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
|
|
|
public SydSM4Mac(SydApi api, int iKeyIndex) {
|
|
this.api = api;
|
|
this.iKeyIndex = iKeyIndex;
|
|
this.pcProcData = pcProcData;
|
|
this.pcMac=pcMac;
|
|
}
|
|
|
|
public SydSM4Mac(SydApi api) {
|
|
this.api = api;
|
|
}
|
|
|
|
public SydSM4Mac update(byte[] pcData) {
|
|
if (null == pcData || 0 == pcData.length) {
|
|
return this;
|
|
}
|
|
int length = pcData.length;
|
|
this.pcdataResult = pcData;
|
|
if (is_finished) {
|
|
throw new SydApiException(0);
|
|
}
|
|
if (length > 4064) {
|
|
throw new SydApiException(0);
|
|
}
|
|
if (0 == sourceWrap.array().length) {
|
|
if (length < 16) {
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
|
|
bufferResult.put(sourceWrap);
|
|
bufferResult.put(pcData);
|
|
sourceWrap = bufferResult;
|
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, 0, pcProcData);
|
|
pcProcData = stringStringLongDataReturn.getProcData();
|
|
} else {
|
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, 0, pcProcData, pcMac);
|
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
|
return this;
|
|
}
|
|
}
|
|
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, 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 {
|
|
if (16 > (pcData.length + sourceWrap.array().length)) {
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + pcData.length);
|
|
bufferResult.put(sourceWrap);
|
|
bufferResult.put(pcData);
|
|
sourceWrap = bufferResult;
|
|
}
|
|
|
|
else if (16<=(pcData.length + sourceWrap.array().length)&&(pcData.length + sourceWrap.array().length) % 16 == 0) {
|
|
int k2 = sourceWrap.array().length+pcData.length;
|
|
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + pcData.length);
|
|
byte[] bytes1 = bytebuffer2ByteArray(sourceWrap);
|
|
bufferResult.put(bytes1);
|
|
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(bytebuffer2ByteArray(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;
|
|
}
|
|
public LongDataReturn disget() {
|
|
|
|
if (is_finished) {
|
|
throw new SydApiException(0);
|
|
}
|
|
this.is_finished = true;
|
|
if (1 == this.iPkgNum) {
|
|
this.iPkgNum = 0;
|
|
} else if (2 == this.iPkgNum) {
|
|
this.iPkgNum = 3;
|
|
}
|
|
if (null == this.pcMac || "".equals(this.pcMac)) {
|
|
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iPkgNum, pcProcData);
|
|
return stringStringLongDataReturn;
|
|
} else {
|
|
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iPkgNum, 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;
|
|
}
|
|
|
|
}
|