This commit is contained in:
cheney 2022-08-31 17:24:02 +08:00
parent fde8ad86e9
commit 7620c538ae
3 changed files with 210 additions and 171 deletions

View File

@ -12,7 +12,7 @@ public class SydApiSM4 {
private int iPkgNum = 1;
private int iFlag;
private boolean is_finished = false;
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
private ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
@ -21,77 +21,87 @@ public class SydApiSM4 {
this.iKeyIndex = iKeyIndex;
}
public void update(byte[] pcData) {
this.pcdataResult = pcData;
if (null == pcData || pcData.length == 0) {
return;
}
int plength = pcData.length;
if (is_finished) {
throw new SydApiException(0);
}
if (plength > 4064) {
throw new SydApiException(0);
}
if (0 == sourceWrap.array().length) {
//加密
if (plength < 16) {
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
if (iFlag == 0 && 1 == iPkgNum) {
throw new SydApiException("解密的首包不能小于 16 字节", 0);
public byte[] update(byte[] pcData) {
try {
this.pcdataResult = pcData;
if (null == pcData || pcData.length == 0) {
return new byte[0];
}
int plength = pcData.length;
if (is_finished) {
throw new SydApiException(0);
}
if (plength > 4064) {
throw new SydApiException(0);
}
if (0 == sourceWrap.array().length) {
//加密
if (plength < 16) {
api.SYD_SM4_LongData(iKeyIndex, new byte[0], iPkgNum, iFlag);
if (iFlag == 0 && 1 == iPkgNum) {
throw new SydApiException("解密的首包不能小于 16 字节", 0);
}
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;
}
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, pcdataResult, iPkgNum, iFlag);
iPkgNum = 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;
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;
api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2;
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
} 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);
return 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;
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;
iPkgNum = 2;
return api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return null;
}
public byte[] finish() {

View File

@ -3,25 +3,22 @@ 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 boolean isFinished;
// 中间数据
private String pcProcData;
private boolean is_finished;
private String pcMac;
private byte[] pcdataResult = new byte[0];
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
private 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) {
@ -29,90 +26,47 @@ public class SydSM4Mac {
}
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;
try {
if (null == pcData || 0 == pcData.length) {
return this;
}
int length = pcData.length;
this.pcdataResult = pcData;
if (isFinished) {
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 + length);
bufferResult.put(sourceWrap);
bufferResult.put(pcData);
sourceWrap = bufferResult;
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
bufferResult.put(sourceWrap);
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;
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;
}
} else {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
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;
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();
@ -122,18 +76,68 @@ public class SydSM4Mac {
pcProcData = stringBooleanLongDataReturn.getProcData();
iPkgNum = 2;
}
} else {
if (16 > pcData.length + sourceWrap.array().length) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
bufferResult.put(sourceWrap);
bufferResult.put(pcData);
sourceWrap = bufferResult;
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + length);
bufferResult.put(sourceWrap);
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(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;
}catch ( Exception e ) {
e.printStackTrace();
throw e;
}
return this;
}
public LongDataReturn disget() {
if (is_finished) {
if (isFinished) {
throw new SydApiException(0);
}
this.is_finished = true;
this.isFinished = true;
if (1 == this.iPkgNum) {
this.iPkgNum = 0;
} else if (2 == this.iPkgNum) {

View File

@ -6,6 +6,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.util.Random;
public class LongDataTest {
@ -13,6 +14,8 @@ public class LongDataTest {
private SydApi api;
@Before
public void init(){
System.setProperty("com.sunyard.sydapi4j.debug", "true");
SydApiBuilder builder = new SydApiBuilder(
new String[]{ "192.168.0.200" },
new int[]{ 8889 },
@ -24,7 +27,7 @@ public class LongDataTest {
}
@Test
public void case1(){
public void sm3_case1(){
SydApiSM3 sm3 = api.initSM3();
sm3.update(null);
String mac = sm3.digest();
@ -32,30 +35,49 @@ public class LongDataTest {
}
@Test
public void case2(){
public void sm3_case2(){
SydApiSM3 sm3 = api.initSM3();
sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"));
String mac = sm3.digest();
Assert.assertEquals("9431C865C84B02C8A7C503F53A6D6C3C66704F8F713D124610314E31B19937F7", mac);
}
for (int i =0 ; i < 1000; i ++) {
@Test
public void sm3_case3(){
SydApiSM3 sm3 = api.initSM3();
sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"));
sm3.update(Util.hexString2Bytes("1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678"));
String mac = sm3.digest();
Assert.assertEquals("2A217F11AD8D113EDCFED070A217DB9328FD00BFF31BA0843E04375C9F0C5B6A", mac);
}
@Test
public void sm3_case4(){
SydApiSM3 sm3 = api.initSM3();
System.out.print( "org=");
for (int i =0 ; i < 100; i ++) {
int lucky = new Random().nextInt(4065);
byte[] data = new byte[lucky];
new Random().nextBytes( data );
System.out.print( Util.bytes2HexString(data) );
sm3.update(data);
}
String mac = sm3.digest();
System.out.println();
System.out.println( mac );
}
@Test
public void case3(){
public void sm3_case5(){
SydApiSM3 sm3 = api.initSM3();
String mac = sm3.digest();
Assert.assertEquals("1AB21D8355CFA17F8E61194831E81A8F22BEC8C728FEFB747ED035EB5082AA2B", mac);
}
@Test
public void case9(){
public void sm4mac_case1(){
SydSM4Mac sydApiMac = api.initSM4Mac(1);
sydApiMac.update(null);
String mac = (String) sydApiMac.disget().getData();
@ -63,17 +85,17 @@ public class LongDataTest {
}
@Test
public void case10(){
public void sm4mac_case2(){
SydSM4Mac sydApiMac = api.initSM4Mac(1);
String mac = (String) sydApiMac.disget().getData();
Assert.assertEquals("FF49F76AD3A71651FE1A8A9F0D2A689E", mac);
}
@Test
public void case11(){
public void sm4mac_case3(){
SydSM4Mac sydApiMac = api.initSM4Mac(1);
for (int i =0 ; i < 1000; i ++) {
for (int i =0 ; i < 10; i ++) {
int lucky = new Random().nextInt(4065);
byte[] data = new byte[lucky];
System.out.println("len="+lucky);
@ -86,6 +108,9 @@ public class LongDataTest {
}
@Test
public void case5(){
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
@ -98,7 +123,7 @@ public class LongDataTest {
public void case6(){
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
for (int i =0 ; i < 1000; i ++) {
for (int i =0 ; i < 10; i ++) {
int lucky = new Random().nextInt(4065);
byte[] data = new byte[lucky];
System.out.println("len="+lucky);