Merge remote-tracking branch 'origin/dev-1.0' into dev-1.0

# Conflicts:
#	src/main/java/com/sunyard/sge/database/SydApiSM4.java
#	src/main/java/com/sunyard/sge/database/SydSM4Mac.java
This commit is contained in:
cheney 2022-08-31 17:24:33 +08:00
commit 75e9b091a8
2 changed files with 171 additions and 181 deletions

View File

@ -12,7 +12,7 @@ public class SydApiSM4 {
private int iPkgNum = 1; private int iPkgNum = 1;
private int iFlag; private int iFlag;
private boolean is_finished = false; private boolean is_finished = false;
private ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]); ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) { public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
@ -21,11 +21,10 @@ public class SydApiSM4 {
this.iKeyIndex = iKeyIndex; this.iKeyIndex = iKeyIndex;
} }
public byte[] update(byte[] pcData) { public void update(byte[] pcData) {
try {
this.pcdataResult = pcData; this.pcdataResult = pcData;
if (null == pcData || pcData.length == 0) { if (null == pcData || pcData.length == 0) {
return new byte[0]; return;
} }
int plength = pcData.length; int plength = pcData.length;
if (is_finished) { if (is_finished) {
@ -59,9 +58,8 @@ public class SydApiSM4 {
bufferResult.put(bytes1); bufferResult.put(bytes1);
sourceWrap = bufferResult; sourceWrap = bufferResult;
} }
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2; iPkgNum = 2;
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
} else { } else {
if (16 > pcData.length + sourceWrap.array().length) { if (16 > pcData.length + sourceWrap.array().length) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
@ -70,13 +68,13 @@ public class SydApiSM4 {
sourceWrap = bufferResult; sourceWrap = bufferResult;
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) { } else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength); ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap); bufferResult.put(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
bufferResult.put(pcData); bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
return 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(SydSM4Mac.bytebuffer2ByteArray(sourceWrap));
bufferResult.put(pcData); bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
ByteBuffer buffer = ByteBuffer.wrap(bytes); ByteBuffer buffer = ByteBuffer.wrap(bytes);
@ -90,18 +88,10 @@ public class SydApiSM4 {
ByteBuffer bufferResults = ByteBuffer.allocate(remainder); ByteBuffer bufferResults = ByteBuffer.allocate(remainder);
bufferResults.put(bytes1); bufferResults.put(bytes1);
sourceWrap = bufferResults; sourceWrap = bufferResults;
api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2; iPkgNum = 2;
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
} }
} }
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return null;
} }
public byte[] finish() { public byte[] finish() {

View File

@ -3,22 +3,25 @@ package com.sunyard.sge.database;
import com.sunyard.SydApiException; import com.sunyard.SydApiException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class SydSM4Mac { public class SydSM4Mac {
private SydApi api; private SydApi api;
private byte[] last_in_data;
private int iKeyIndex; private int iKeyIndex;
private int iPkgNum = 1; private int iPkgNum = 1;
private boolean isFinished;
// 中间数据
private String pcProcData; private String pcProcData;
private boolean is_finished;
private String pcMac;
private ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]); private byte[] pcdataResult = new byte[0];
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
public SydSM4Mac(SydApi api, int iKeyIndex) { public SydSM4Mac(SydApi api, int iKeyIndex) {
this.api = api; this.api = api;
this.iKeyIndex = iKeyIndex; this.iKeyIndex = iKeyIndex;
this.pcProcData = pcProcData;
this.pcMac=pcMac;
} }
public SydSM4Mac(SydApi api) { public SydSM4Mac(SydApi api) {
@ -26,14 +29,12 @@ public class SydSM4Mac {
} }
public SydSM4Mac update(byte[] pcData) { public SydSM4Mac update(byte[] pcData) {
try {
if (null == pcData || 0 == pcData.length) { if (null == pcData || 0 == pcData.length) {
return this; return this;
} }
int length = pcData.length; int length = pcData.length;
this.pcdataResult = pcData; this.pcdataResult = pcData;
if (isFinished) { if (is_finished) {
throw new SydApiException(0); throw new SydApiException(0);
} }
if (length > 4064) { if (length > 4064) {
@ -77,14 +78,18 @@ public class SydSM4Mac {
iPkgNum = 2; iPkgNum = 2;
} }
} else { } else {
if (16 > pcData.length + sourceWrap.array().length) { if (16 > (pcData.length + sourceWrap.array().length)) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length); ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + pcData.length);
bufferResult.put(sourceWrap); bufferResult.put(sourceWrap);
bufferResult.put(pcData); bufferResult.put(pcData);
sourceWrap = bufferResult; sourceWrap = bufferResult;
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) { }
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
bufferResult.put(sourceWrap); 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); bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
if (null == this.pcMac || "".equals(this.pcMac)) { if (null == this.pcMac || "".equals(this.pcMac)) {
@ -98,7 +103,7 @@ public class SydSM4Mac {
} }
} else { } else {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length); ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
bufferResult.put(sourceWrap); bufferResult.put(bytebuffer2ByteArray(sourceWrap));
bufferResult.put(pcData); bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult); byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
ByteBuffer buffer = ByteBuffer.wrap(bytes); ByteBuffer buffer = ByteBuffer.wrap(bytes);
@ -124,20 +129,13 @@ public class SydSM4Mac {
} }
} }
return this; return this;
}catch ( Exception e ) {
e.printStackTrace();
throw e;
} }
}
public LongDataReturn disget() { public LongDataReturn disget() {
if (isFinished) { if (is_finished) {
throw new SydApiException(0); throw new SydApiException(0);
} }
this.isFinished = true; this.is_finished = true;
if (1 == this.iPkgNum) { if (1 == this.iPkgNum) {
this.iPkgNum = 0; this.iPkgNum = 0;
} else if (2 == this.iPkgNum) { } else if (2 == this.iPkgNum) {
@ -150,6 +148,7 @@ public class SydSM4Mac {
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iPkgNum, pcProcData, pcMac); LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, this.pcdataResult, iPkgNum, pcProcData, pcMac);
return stringBooleanLongDataReturn; return stringBooleanLongDataReturn;
} }
} }
public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) { public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) {
@ -165,4 +164,5 @@ public class SydSM4Mac {
} }
return bytes; return bytes;
} }
} }