debug
This commit is contained in:
parent
fde8ad86e9
commit
7620c538ae
@ -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;
|
||||||
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
private ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
|
||||||
|
|
||||||
|
|
||||||
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
|
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
|
||||||
@ -21,10 +21,11 @@ public class SydApiSM4 {
|
|||||||
this.iKeyIndex = iKeyIndex;
|
this.iKeyIndex = iKeyIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(byte[] pcData) {
|
public byte[] update(byte[] pcData) {
|
||||||
|
try {
|
||||||
this.pcdataResult = pcData;
|
this.pcdataResult = pcData;
|
||||||
if (null == pcData || pcData.length == 0) {
|
if (null == pcData || pcData.length == 0) {
|
||||||
return;
|
return new byte[0];
|
||||||
}
|
}
|
||||||
int plength = pcData.length;
|
int plength = pcData.length;
|
||||||
if (is_finished) {
|
if (is_finished) {
|
||||||
@ -58,8 +59,9 @@ 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);
|
||||||
@ -71,7 +73,7 @@ public class SydApiSM4 {
|
|||||||
bufferResult.put(sourceWrap);
|
bufferResult.put(sourceWrap);
|
||||||
bufferResult.put(pcData);
|
bufferResult.put(pcData);
|
||||||
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
|
||||||
api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
|
return 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(sourceWrap);
|
||||||
@ -88,10 +90,18 @@ 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() {
|
||||||
|
|||||||
@ -3,25 +3,22 @@ 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 byte[] pcdataResult = new byte[0];
|
private ByteBuffer sourceWrap = ByteBuffer.wrap(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) {
|
||||||
@ -29,12 +26,14 @@ 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 (is_finished) {
|
if (isFinished) {
|
||||||
throw new SydApiException(0);
|
throw new SydApiException(0);
|
||||||
}
|
}
|
||||||
if (length > 4064) {
|
if (length > 4064) {
|
||||||
@ -125,15 +124,20 @@ public class SydSM4Mac {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
}catch ( Exception e ) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public LongDataReturn disget() {
|
public LongDataReturn disget() {
|
||||||
|
|
||||||
if (is_finished) {
|
if (isFinished) {
|
||||||
throw new SydApiException(0);
|
throw new SydApiException(0);
|
||||||
}
|
}
|
||||||
this.is_finished = true;
|
this.isFinished = 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) {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import org.junit.Assert;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class LongDataTest {
|
public class LongDataTest {
|
||||||
@ -13,6 +14,8 @@ public class LongDataTest {
|
|||||||
private SydApi api;
|
private SydApi api;
|
||||||
@Before
|
@Before
|
||||||
public void init(){
|
public void init(){
|
||||||
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
SydApiBuilder builder = new SydApiBuilder(
|
SydApiBuilder builder = new SydApiBuilder(
|
||||||
new String[]{ "192.168.0.200" },
|
new String[]{ "192.168.0.200" },
|
||||||
new int[]{ 8889 },
|
new int[]{ 8889 },
|
||||||
@ -24,7 +27,7 @@ public class LongDataTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case1(){
|
public void sm3_case1(){
|
||||||
SydApiSM3 sm3 = api.initSM3();
|
SydApiSM3 sm3 = api.initSM3();
|
||||||
sm3.update(null);
|
sm3.update(null);
|
||||||
String mac = sm3.digest();
|
String mac = sm3.digest();
|
||||||
@ -32,30 +35,49 @@ public class LongDataTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case2(){
|
public void sm3_case2(){
|
||||||
SydApiSM3 sm3 = api.initSM3();
|
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);
|
int lucky = new Random().nextInt(4065);
|
||||||
byte[] data = new byte[lucky];
|
byte[] data = new byte[lucky];
|
||||||
new Random().nextBytes( data );
|
new Random().nextBytes( data );
|
||||||
|
System.out.print( Util.bytes2HexString(data) );
|
||||||
sm3.update(data);
|
sm3.update(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
String mac = sm3.digest();
|
String mac = sm3.digest();
|
||||||
|
System.out.println();
|
||||||
System.out.println( mac );
|
System.out.println( mac );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case3(){
|
public void sm3_case5(){
|
||||||
SydApiSM3 sm3 = api.initSM3();
|
SydApiSM3 sm3 = api.initSM3();
|
||||||
String mac = sm3.digest();
|
String mac = sm3.digest();
|
||||||
Assert.assertEquals("1AB21D8355CFA17F8E61194831E81A8F22BEC8C728FEFB747ED035EB5082AA2B", mac);
|
Assert.assertEquals("1AB21D8355CFA17F8E61194831E81A8F22BEC8C728FEFB747ED035EB5082AA2B", mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case9(){
|
public void sm4mac_case1(){
|
||||||
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
||||||
sydApiMac.update(null);
|
sydApiMac.update(null);
|
||||||
String mac = (String) sydApiMac.disget().getData();
|
String mac = (String) sydApiMac.disget().getData();
|
||||||
@ -63,17 +85,17 @@ public class LongDataTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case10(){
|
public void sm4mac_case2(){
|
||||||
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
||||||
String mac = (String) sydApiMac.disget().getData();
|
String mac = (String) sydApiMac.disget().getData();
|
||||||
Assert.assertEquals("FF49F76AD3A71651FE1A8A9F0D2A689E", mac);
|
Assert.assertEquals("FF49F76AD3A71651FE1A8A9F0D2A689E", mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case11(){
|
public void sm4mac_case3(){
|
||||||
SydSM4Mac sydApiMac = api.initSM4Mac(1);
|
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);
|
int lucky = new Random().nextInt(4065);
|
||||||
byte[] data = new byte[lucky];
|
byte[] data = new byte[lucky];
|
||||||
System.out.println("len="+lucky);
|
System.out.println("len="+lucky);
|
||||||
@ -86,6 +108,9 @@ public class LongDataTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void case5(){
|
public void case5(){
|
||||||
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
|
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
|
||||||
@ -98,7 +123,7 @@ public class LongDataTest {
|
|||||||
public void case6(){
|
public void case6(){
|
||||||
SydApiSM4 sydApiSM4 = api.initSM4(api, 1,1);
|
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);
|
int lucky = new Random().nextInt(4065);
|
||||||
byte[] data = new byte[lucky];
|
byte[] data = new byte[lucky];
|
||||||
System.out.println("len="+lucky);
|
System.out.println("len="+lucky);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user