接口文档更新

This commit is contained in:
junj2.liang
2022-09-05 10:22:27 +08:00
parent 43a75755ff
commit f77b5daa93
6 changed files with 356 additions and 229 deletions
@@ -272,7 +272,7 @@ public interface SydApi {
);
public SydApiSM4 initSM4( int iKeyIndex, int ende);
public SydApiSM423 initSM4(int iKeyIndex, int ende);
public SydApiSM3 initSM3();
@@ -3,14 +3,12 @@ package com.sunyard.sge.database;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import com.sunyard.sge.database.pool.HsmLinkInfo;
import com.sunyard.sge.database.pool.SydApiPool;
import com.sunyard.sge.log.LogFactory;
import com.sunyard.sge.pool.ObjectInPool;
import com.sunyard.util.SYMUtil;
import org.apache.logging.log4j.Logger;
import racal.sunyard.main.SydApi4j;
import javax.annotation.CheckForNull;
import java.util.Arrays;
import java.util.List;
@@ -132,7 +130,7 @@ public class SydApi4Database implements SydApi {
if (null == this.hsms || this.hsms.length < 1) {
throw new SydApiException(-1);
}
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
updateLastUseTime();
for (SydApi4j api : this.hsms) {
@@ -191,7 +189,7 @@ public class SydApi4Database implements SydApi {
}
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
updateLastUseTime();
for (SydApi4j api : this.hsms) {
if (null == api) {
@@ -254,7 +252,7 @@ public class SydApi4Database implements SydApi {
updateLastUseTime();
String keyIndex = String.format("%08d", iKeyIndex);
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) {
if (null == api) {
continue;
@@ -335,7 +333,7 @@ public class SydApi4Database implements SydApi {
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
}
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) {
if (null == api) {
continue;
@@ -394,7 +392,7 @@ public class SydApi4Database implements SydApi {
updateLastUseTime();
String keyIndex = String.format("%08d", iKeyIndex);
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) {
if (null == api) {
continue;
@@ -477,7 +475,7 @@ public class SydApi4Database implements SydApi {
for (int i = 0; i < iKeyIndex.length; i++) {
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
}
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) {
if (null == api) {
continue;
@@ -545,7 +543,7 @@ public class SydApi4Database implements SydApi {
keys[i] = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex[i]));
}
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
for (SydApi4j api : this.hsms) {
if (null == api) {
continue;
@@ -615,7 +613,7 @@ public class SydApi4Database implements SydApi {
if (Consts.PkgNumAll != iPkgNum && Consts.PkgNumHead != iPkgNum) {
iv = pcProcData.getBytes();
}
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
updateLastUseTime();
for (SydApi4j api : this.hsms) {
if (null == api) {
@@ -714,7 +712,7 @@ public class SydApi4Database implements SydApi {
String keyIndex = "SK" + SYMUtil.genPrivateKeyMark("000000", "00A", String.format("%08d", iKeyIndex));
log.debug("\"SK\" + SYMUtil.genPrivateKeyMark方法返回值:{}", keyIndex);
SydApiException le = new SydApiException(0);
SydApiException le = new SydApiException(-1);
updateLastUseTime();
for (SydApi4j api : this.hsms) {
if (null == api) {
@@ -775,9 +773,9 @@ public class SydApi4Database implements SydApi {
@Override
public SydApiSM4 initSM4(int iKeyIndex, int ende) {
public SydApiSM423 initSM4(int iKeyIndex, int ende) {
return new SydApiSM4(this,iKeyIndex,ende);
return new SydApiSM423(this,iKeyIndex,ende);
}
public SydApiSM3 initSM3() {
@@ -2,6 +2,7 @@ package com.sunyard.sge.database;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import com.sunyard.sge.bytes.BytesQueue;
import java.nio.ByteBuffer;
@@ -14,8 +15,7 @@ public class SydApiSM4 {
private int iFlag;
private boolean is_finished = false;
private byte[] dataResult = new byte[0];
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
BytesQueue queue = new BytesQueue();
public SydApiSM4(SydApi api, int iKeyIndex, int endeFlag) {
@@ -38,115 +38,93 @@ public class SydApiSM4 {
throw new SydApiException(0);
}
if (iFlag == 1) {
if (0 == sourceWrap.array().length) {
if (0 == queue.getLength()) {
//加密
if (plength < 16) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap);
bufferResult.put(pcData);
sourceWrap = bufferResult;
queue.push(pcData);
byte[] pop = queue.pop(16, 0);
pcdataResult = pop;
return new byte[0];
}
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;
queue.push(pcData);
byte[] pop = queue.pop(16, 0);
pcdataResult = pop;
}
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2;
return bytes;
} else {
if (16 > pcData.length + sourceWrap.array().length) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap.array());
bufferResult.put(pcData);
sourceWrap = bufferResult;
if (16 > (pcData.length + queue.getLength())) {
queue.push(pcData);
return new byte[0];
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap.array());
bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
byte[] bytes1 = api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
iPkgNum = 2;
return bytes1;
} else {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap.array());
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;
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2;
return bytes2;
}
queue.push(pcData);
byte[] pop = queue.pop(16, 0);
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
iPkgNum = 2;
return bytes2;
}
}
else {
ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length );
byte[] waitData = sourceWrap.array();
nSourceWrap.put( waitData );
nSourceWrap.put( pcData );
sourceWrap = nSourceWrap;
System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
int totalLen = sourceWrap.array().length;
if ( totalLen / 16 < 2){
} else {
if (null==queue||null==pcData){
throw new IllegalArgumentException();
}
queue.push(pcData);
// int totalLen = queue.getLength() + pcData.length;
if ((queue.getLength()) / 16 < 2) {
return new byte[0];
}
int com = totalLen % 16;
int sendBuffLen = totalLen - com -16;
byte[] sendBuff = new byte[sendBuffLen];
sourceWrap.position(0);
sourceWrap.get( sendBuff , 0, sendBuff.length );
byte[] waitBuff = new byte[ totalLen - sendBuffLen ];
sourceWrap.get( waitBuff , 0, waitBuff.length );
System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d",
pcData.length,
totalLen,
sendBuff.length,
sendBuff.length % 16,
iPkgNum)
);
System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) );
if( 0x00 == waitBuff[0] ) {
System.out.println("eee");
}
sourceWrap = ByteBuffer.wrap( waitBuff );
System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag);
byte[] pop = queue.pop(16, 16);
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
iPkgNum = 2;
return bytes2;
// ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length );
// byte[] waitData = sourceWrap.array();
// nSourceWrap.put( waitData );
// nSourceWrap.put( pcData );
// sourceWrap = nSourceWrap;
//
// System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
//
// int totalLen = sourceWrap.array().length;
// if ( totalLen / 16 < 2){
// return new byte[0];
// }
//
//
// int com = totalLen % 16;
// int sendBuffLen = totalLen - com -16;
// byte[] sendBuff = new byte[sendBuffLen];
//
//
//
// sourceWrap.position(0);
// sourceWrap.get( sendBuff , 0, sendBuff.length );
// byte[] waitBuff = new byte[ totalLen - sendBuffLen ];
// sourceWrap.get( waitBuff , 0, waitBuff.length );
//
// System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d",
// pcData.length,
// totalLen,
// sendBuff.length,
// sendBuff.length % 16,
// iPkgNum)
// );
//
// System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) );
//
// if( 0x00 == waitBuff[0] ) {
// System.out.println("eee");
// }
// sourceWrap = ByteBuffer.wrap( waitBuff );
// System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
// byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag);
//
// iPkgNum = 2;
// return bytes2;
}
}
@@ -162,23 +140,22 @@ public class SydApiSM4 {
this.iPkgNum = 3;
}
if (iFlag == 0){
byte[] d = sourceWrap.array();
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag);
sourceWrap = null;
byte[] pop = queue.pop();
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, pop, iPkgNum, iFlag);
return bytes;
}
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), iPkgNum, iFlag);
sourceWrap = null;
return bytes;
// byte[] bytes = api.SYD_SM4_LongData(iKeyIndex, , iPkgNum, iFlag);
// sourceWrap = null;
// return bytes;
}
public ByteBuffer bufferResult(int plength, byte[] pcData) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap);
bufferResult.put(pcData);
return bufferResult;
}
// public ByteBuffer bufferResult(int plength, byte[] pcData) {
// ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
// bufferResult.put(sourceWrap);
// bufferResult.put(pcData);
// return bufferResult;
// }
}
@@ -0,0 +1,186 @@
package com.sunyard.sge.database;
import com.sunyard.SydApiException;
import com.sunyard.proto.Util;
import java.nio.ByteBuffer;
public class SydApiSM423 {
private SydApi api;
private int iKeyIndex;
private byte[] pcdataResult = new byte[0];
private int iPkgNum = 1;
private int iFlag;
private boolean is_finished = false;
private byte[] dataResult = new byte[0];
ByteBuffer sourceWrap = ByteBuffer.wrap(new byte[0]);
public SydApiSM423(SydApi api, int iKeyIndex, int endeFlag) {
this.api = api;
this.iFlag = endeFlag;
this.iKeyIndex = iKeyIndex;
}
public byte[] update(byte[] pcData) {
this.pcdataResult = pcData;
// this.dataResult = 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 (iFlag == 1) {
if (0 == sourceWrap.array().length) {
//加密
if (plength < 16) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap);
bufferResult.put(pcData);
sourceWrap = bufferResult;
return new byte[0];
}
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;
return bytes;
} else {
if (16 > pcData.length + sourceWrap.array().length) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap.array());
bufferResult.put(pcData);
sourceWrap = bufferResult;
return new byte[0];
} else if ((pcData.length + sourceWrap.array().length) % 16 == 0) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap.array());
bufferResult.put(pcData);
byte[] bytes = SydSM4Mac.bytebuffer2ByteArray(bufferResult);
byte[] bytes1 = api.SYD_SM4_LongData(iKeyIndex, bytes, iPkgNum, iFlag);
iPkgNum = 2;
return bytes1;
} else {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap.array());
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;
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, pcdataResult, iPkgNum, iFlag);
iPkgNum = 2;
return bytes2;
}
}
}
else {
ByteBuffer nSourceWrap = ByteBuffer.allocate( sourceWrap.array().length + pcData.length );
byte[] waitData = sourceWrap.array();
nSourceWrap.put( waitData );
nSourceWrap.put( pcData );
sourceWrap = nSourceWrap;
System.out.println("new sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
int totalLen = sourceWrap.array().length;
if ( totalLen / 16 < 2){
return new byte[0];
}
int com = totalLen % 16;
int sendBuffLen = totalLen - com -16;
byte[] sendBuff = new byte[sendBuffLen];
sourceWrap.position(0);
sourceWrap.get( sendBuff , 0, sendBuff.length );
byte[] waitBuff = new byte[ totalLen - sendBuffLen ];
sourceWrap.get( waitBuff , 0, waitBuff.length );
System.out.println( String.format("debug inDataLen=%d totalDataLen=%d sendDataLen=%d 16=%d iPkgNum=%d",
pcData.length,
totalLen,
sendBuff.length,
sendBuff.length % 16,
iPkgNum)
);
System.out.println( "waitBuff=" + Util.bytes2HexString( waitBuff ) );
if( 0x00 == waitBuff[0] ) {
System.out.println("eee");
}
sourceWrap = ByteBuffer.wrap( waitBuff );
System.out.println("sourceWrap=" + Util.bytes2HexString(sourceWrap.array()));
byte[] bytes2 = api.SYD_SM4_LongData(iKeyIndex, sendBuff, iPkgNum, iFlag);
iPkgNum = 2;
return bytes2;
}
}
public byte[] finish() {
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 (iFlag == 0){
byte[] d = sourceWrap.array();
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,d, iPkgNum, iFlag);
sourceWrap = null;
return bytes;
}
byte[] bytes = api.SYD_SM4_LongData(iKeyIndex,sourceWrap.array(), iPkgNum, iFlag);
sourceWrap = null;
return bytes;
}
public ByteBuffer bufferResult(int plength, byte[] pcData) {
ByteBuffer bufferResult = ByteBuffer.allocate(sourceWrap.array().length + plength);
bufferResult.put(sourceWrap);
bufferResult.put(pcData);
return bufferResult;
}
}