114 lines
3.6 KiB
Java
114 lines
3.6 KiB
Java
package com.sunyard.sge.database;
|
|
|
|
import com.sunyard.SydApiException;
|
|
import com.sunyard.sge.bytes.BytesQueue;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.util.Arrays;
|
|
|
|
public class SydSM4Mac implements ISM4Mac {
|
|
private final SydApi api;
|
|
private final int iKeyIndex;
|
|
private int iPkgNum = 1;
|
|
private byte[] pcProcData;
|
|
private boolean is_finished;
|
|
private byte[] pcMac;
|
|
private BytesQueue queue = new BytesQueue();
|
|
|
|
public SydSM4Mac(SydApi api, int iKeyIndex) {
|
|
this.api = api;
|
|
this.iKeyIndex = iKeyIndex;
|
|
this.pcProcData = null;
|
|
}
|
|
|
|
@Override
|
|
public void update(byte[] pcData) {
|
|
if (null == pcData || 0 == pcData.length) {
|
|
return;
|
|
}
|
|
int length = pcData.length;
|
|
byte[] pcdataResult = pcData;
|
|
if (is_finished) {
|
|
throw new SydApiException(0x8007);
|
|
}
|
|
if (length > 4064) {
|
|
throw new SydApiException(0x8006);
|
|
}
|
|
if (0 == queue.getLength()) {
|
|
if (length < 16) {
|
|
queue.push(pcData);
|
|
}
|
|
if (length % 16 != 0) {
|
|
queue.push(pcData);
|
|
pcdataResult = queue.pop(16, 0);
|
|
}
|
|
if (null == this.pcMac || this.pcMac.length == 0) {
|
|
LongDataReturn<byte[], byte[]> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData);
|
|
pcProcData = stringStringLongDataReturn.getProcData();
|
|
iPkgNum = 2;
|
|
} else {
|
|
LongDataReturn<byte[], Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData, pcMac);
|
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
|
iPkgNum = 2;
|
|
}
|
|
} else {
|
|
if (16 > (pcData.length + queue.getLength())) {
|
|
queue.push(pcData);
|
|
}
|
|
|
|
queue.push(pcData);
|
|
byte[] pop = queue.pop(16, 0);
|
|
if (null == this.pcMac || this.pcMac.length == 0 ) {
|
|
LongDataReturn<byte[], byte[]> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData);
|
|
pcProcData = stringStringLongDataReturn.getProcData();
|
|
iPkgNum = 2;
|
|
} else {
|
|
LongDataReturn<byte[], Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData, pcMac);
|
|
pcProcData = stringBooleanLongDataReturn.getProcData();
|
|
iPkgNum = 2;
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean checkDigest(byte[] mac) {
|
|
return mac != null && Arrays.equals(this.pcMac, mac);
|
|
}
|
|
|
|
@Override
|
|
public byte[] digest() {
|
|
|
|
if (is_finished) {
|
|
throw new SydApiException(0x8007);
|
|
}
|
|
this.is_finished = true;
|
|
if (1 == this.iPkgNum) {
|
|
this.iPkgNum = 0;
|
|
} else if (2 == this.iPkgNum) {
|
|
this.iPkgNum = 3;
|
|
}
|
|
byte[] pop = queue.pop();
|
|
|
|
LongDataReturn<byte[], byte[]> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData);
|
|
return stringStringLongDataReturn.getData();
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
}
|