mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
修复bytehelper & VlInt中的已知bug
This commit is contained in:
1 parent
227d10a391
commit
2ad446ca2c
29 files changed
+315
-80
No files matched your search
@@ -83,7 +83,12 @@ public class CongestionMark implements TlvComponentBase,InitialAble, IEncodingAb
|
||||
}
|
||||
|
||||
// 读取 CongestionMark
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -83,7 +83,12 @@ public class FreshnessPeriod implements TlvComponentBase,InitialAble, IEncodingA
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -78,7 +78,12 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
}
|
||||
|
||||
// 读取 HopLimit
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ package component;
|
||||
|
||||
import encoding.*;
|
||||
import util.ByteHelper;
|
||||
import util.ByteHelperException;
|
||||
import util.StringHelper;
|
||||
|
||||
// @Description:
|
||||
@@ -58,7 +59,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
this.identifierBlock=new Block();
|
||||
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
|
||||
this.identifierBlock.setLength(new VlInt(nameComponent.length()+1));
|
||||
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
byte[] value= new byte[0];
|
||||
try {
|
||||
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
value[0]=MarkerString;
|
||||
System.arraycopy(nameComponent.getBytes(),0,value,1,nameComponent.getBytes().length);
|
||||
this.identifierBlock.setValue(value);
|
||||
@@ -74,7 +80,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
this.identifierBlock=new Block();
|
||||
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
|
||||
this.identifierBlock.setLength(new VlInt(new VlInt(intValue).size()+1));
|
||||
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
byte[] value= new byte[0];
|
||||
try {
|
||||
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
value[0]=MarkerNonNegativeInteger;
|
||||
System.arraycopy(new VlInt(intValue).getVlIntBytes(),0,value,1,new VlInt(intValue).size());
|
||||
this.identifierBlock.setValue(value);
|
||||
@@ -90,7 +101,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
this.identifierBlock=new Block();
|
||||
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
|
||||
this.identifierBlock.setLength(new VlInt(arr.length+1));
|
||||
byte[] value=new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
byte[] value= new byte[0];
|
||||
try {
|
||||
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
value[0]=MarkerByteArray;
|
||||
System.arraycopy(arr,0,value,1,arr.length);
|
||||
this.identifierBlock.setValue(value);
|
||||
@@ -175,7 +191,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
this.identifierBlock=new Block(block);
|
||||
|
||||
//todo:越界问题
|
||||
int length=this.identifierBlock.getLength().getVlIntValue2Int();
|
||||
int length= 0;
|
||||
try {
|
||||
length = this.identifierBlock.getLength().getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
byte[] value=this.identifierBlock.getValue();
|
||||
|
||||
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
|
||||
@@ -190,7 +211,12 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
|
||||
this.marker=value[0];
|
||||
|
||||
// 根据不同类型的Marker,给不同的成员赋值
|
||||
byte[] v=ByteHelper.getTailBytes(value,1);
|
||||
byte[] v= new byte[0];
|
||||
try {
|
||||
v = ByteHelper.getTailBytes(value,1);
|
||||
} catch (ByteHelperException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(isString()){
|
||||
this.stringValue=new String(v);
|
||||
}else if(isNonNegativeInteger()){
|
||||
|
||||
@@ -83,7 +83,12 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -83,7 +83,12 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -93,7 +93,12 @@ public class Nonce implements TlvComponentBase,InitialAble, IEncodingAble {
|
||||
}
|
||||
|
||||
// 读取 value
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class SignatureTypeValue{
|
||||
}
|
||||
}
|
||||
public class SignatureType implements TlvComponentBase, IEncodingAble {
|
||||
// private SignatureAlgorithm value;
|
||||
// private SignatureAlgorithm value;
|
||||
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) {
|
||||
|
||||
@@ -103,7 +103,12 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
|
||||
return false;
|
||||
}
|
||||
|
||||
int len=block.getLength().getVlIntValue2Int();
|
||||
int len= 0;
|
||||
try {
|
||||
len = block.getLength().getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.value=new byte[len];
|
||||
System.arraycopy(block.getValue(),0,this.value,0,len);
|
||||
return true;
|
||||
|
||||
@@ -91,7 +91,12 @@ public class TTL implements IEncodingAble {
|
||||
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvTTL))){
|
||||
return false;
|
||||
}
|
||||
long value=TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
long value= 0;
|
||||
try {
|
||||
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(value<0){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package encoding;
|
||||
|
||||
import util.ByteHelper;
|
||||
import util.ByteHelperException;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -43,7 +44,12 @@ public class Block {
|
||||
}
|
||||
|
||||
// 读取length
|
||||
VlInt tlvLength=TLV.readVarNumber(buffer,new VlInt(tlvType.size()));
|
||||
VlInt tlvLength= null;
|
||||
try {
|
||||
tlvLength = TLV.readVarNumber(buffer,new VlInt(tlvType.size()));
|
||||
} catch (ByteHelperException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(tlvLength==null){
|
||||
return null;
|
||||
}
|
||||
@@ -197,7 +203,7 @@ public class Block {
|
||||
* 2. 如果TLV的value不为空,且不存在嵌套的子TLV,则会抛出错误
|
||||
* @return
|
||||
*/
|
||||
public boolean parseSubElements(){
|
||||
public boolean parseSubElements() {
|
||||
if((this.value.length==0)||this.elements.length()!=0){
|
||||
return true;
|
||||
}
|
||||
@@ -213,7 +219,12 @@ public class Block {
|
||||
start+=tlvType.size();
|
||||
|
||||
// 读取 TLV-LENGTH
|
||||
VlInt tlvLength=TLV.readVarNumber(this.value,new VlInt(start));
|
||||
VlInt tlvLength= null;
|
||||
try {
|
||||
tlvLength = TLV.readVarNumber(this.value,new VlInt(start));
|
||||
} catch (ByteHelperException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(tlvType==null){
|
||||
this.clearElements();
|
||||
return false;
|
||||
@@ -227,14 +238,19 @@ public class Block {
|
||||
}
|
||||
|
||||
// 创建一个子 TLV Block 并添加到elements当中
|
||||
byte[] subBlockValue=new byte[tlvLength.getVlIntValue2Int()];
|
||||
System.arraycopy(this.value,start,subBlockValue,0,tlvLength.getVlIntValue2Int());
|
||||
Block block=createBlockByTypeLengthBuffer(tlvType,tlvLength,subBlockValue,true);
|
||||
if(block==null){
|
||||
return false;
|
||||
byte[] subBlockValue= new byte[0];
|
||||
try {
|
||||
subBlockValue = new byte[tlvLength.getVlIntValue2Int()];
|
||||
System.arraycopy(this.value,start,subBlockValue,0,tlvLength.getVlIntValue2Int());
|
||||
Block block=createBlockByTypeLengthBuffer(tlvType,tlvLength,subBlockValue,true);
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.addElement(block);
|
||||
start+=tlvLength.getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.addElement(block);
|
||||
start+=tlvLength.getVlIntValue2Int();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package encoding;
|
||||
|
||||
|
||||
import util.ByteHelper;
|
||||
import util.ByteHelperException;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -72,13 +73,15 @@ public class TLV {
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static VlInt readVarNumber(byte[] buffer,VlInt start){
|
||||
public static VlInt readVarNumber(byte[] buffer,VlInt start) throws ByteHelperException {
|
||||
VlInt length=new VlInt(buffer.length);
|
||||
if(start.compareTo(length)>=0){
|
||||
return null;
|
||||
}
|
||||
|
||||
int firstbyte=ByteHelper.uint8ToInt(ByteHelper.getFirstByte(buffer,start));
|
||||
int firstbyte= 0;
|
||||
firstbyte = ByteHelper.uint8ToInt(ByteHelper.getFirstByte(buffer,start));
|
||||
|
||||
if(firstbyte<241){
|
||||
return new VlInt(firstbyte);
|
||||
}else if(firstbyte==241){
|
||||
@@ -108,7 +111,12 @@ public class TLV {
|
||||
* @return
|
||||
*/
|
||||
public static VlInt readType(byte[] buffer,VlInt start){
|
||||
VlInt tlvType=readVarNumber(buffer,start);
|
||||
VlInt tlvType= null;
|
||||
try {
|
||||
tlvType = readVarNumber(buffer,start);
|
||||
} catch (ByteHelperException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(tlvType==null||tlvType.isEqual(0)||tlvType.compareTo(MaxTlvNum)>0){
|
||||
return null;
|
||||
|
||||
@@ -4,6 +4,7 @@ package encoding;
|
||||
import util.ByteHelper;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static java.lang.Math.pow;
|
||||
|
||||
@@ -117,22 +118,23 @@ public class VlInt {
|
||||
* 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
|
||||
* @return
|
||||
*/
|
||||
public int getVlIntValue2Int(){
|
||||
public int getVlIntValue2Int() throws VlIntException {
|
||||
if(this.size()<4){
|
||||
return VlIntValue.intValue();
|
||||
}
|
||||
return Integer.parseInt(null);
|
||||
throw new VlIntException("value大于int能表示的范围");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将vlint转为一个long输出:需要判断size是不是小于8(5)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public long getVlIntValue2Long(){
|
||||
if(this.size()<8){
|
||||
public long getVlIntValue2Long() throws VlIntException {
|
||||
if(this.size()<= 9){
|
||||
return VlIntValue.longValue();
|
||||
}
|
||||
return Long.parseLong(null);
|
||||
throw new VlIntException("value大于long能表示的范围");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,8 +192,8 @@ public class VlInt {
|
||||
* @param vlInt
|
||||
* @return
|
||||
*/
|
||||
public int subtract(VlInt vlInt){
|
||||
return this.VlIntValue.subtract(vlInt.VlIntValue).intValue();
|
||||
public long subtract(VlInt vlInt){
|
||||
return this.VlIntValue.subtract(vlInt.VlIntValue).longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,11 +201,11 @@ public class VlInt {
|
||||
* @param vlInt
|
||||
* @return
|
||||
*/
|
||||
public int add(VlInt vlInt){
|
||||
return this.VlIntValue.add(vlInt.VlIntValue).intValue();
|
||||
public long add(VlInt vlInt){
|
||||
return this.VlIntValue.add(vlInt.VlIntValue).longValue();
|
||||
}
|
||||
public int add(int i_int){
|
||||
return this.VlIntValue.intValue()+i_int;
|
||||
public long add(int i_int){
|
||||
return this.VlIntValue.longValue()+i_int;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,6 +265,16 @@ public class VlInt {
|
||||
return vlintValue;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "VlInt{" +
|
||||
"VlIntBytes=" + Arrays.toString(VlIntBytes) +
|
||||
", VlIntValue=" + VlIntValue +
|
||||
", size=" + size +
|
||||
'}';
|
||||
}
|
||||
|
||||
// 测试
|
||||
public static void main(String[] args){
|
||||
// BigInteger bigInteger=new BigInteger("3");
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package encoding;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:38 2021/03/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class VlIntException extends Exception{
|
||||
public VlIntException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -165,7 +165,7 @@ public class Certificate {
|
||||
"version=" + version +
|
||||
", serialNumber=" + serialNumber +
|
||||
", publicKey=" + publicKey +
|
||||
", signature=" + ByteUtils.toHexString(signature) +
|
||||
", signature=" + (signature != null ? ByteUtils.toHexString(signature) : "null") +
|
||||
", signatureAlgorithm=" + signatureAlgorithm +
|
||||
", publicKeyAlgorithm=" + publicKeyAlgorithm +
|
||||
", issueTo='" + issueTo + '\'' +
|
||||
|
||||
@@ -46,7 +46,7 @@ public class InnerCertificate {
|
||||
return "InnerCertificate{" +
|
||||
"tbsCertificate=" + tbsCertificate.toString() +
|
||||
", signatureAlgorithm=" + signatureAlgorithm +
|
||||
", signatureValue=" + ByteUtils.toHexString(signatureValue) +
|
||||
", signatureValue=" + (signatureValue != null ? ByteUtils.toHexString(signatureValue) : "null") +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -152,7 +152,7 @@ public class TbsCertificate {
|
||||
return "TbsCertificate{" +
|
||||
"version=" + version +
|
||||
", serialNumber=" + serialNumber +
|
||||
", publicKey=" + ByteUtils.toHexString(publicKey) +
|
||||
", publicKey=" + (publicKey != null ?ByteUtils.toHexString(publicKey) : "null") +
|
||||
", signatureAlgorithm=" + signatureAlgorithm +
|
||||
", publicKeyAlgorithm=" + publicKeyAlgorithm +
|
||||
", issueTo='" + issueTo + '\'' +
|
||||
|
||||
@@ -13,6 +13,7 @@ import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
*/
|
||||
public class KeyUtils {
|
||||
/**
|
||||
* 可以理解为公钥的"构造函数"
|
||||
* 返回的数据类型为PublicKeyInterface接口, 必要时调用者需要手动将类型强制转化为需要的类型
|
||||
* @param bytesOfPublicKey 公钥的bytes数组
|
||||
* @param algorithm 公钥类型
|
||||
@@ -33,7 +34,16 @@ public class KeyUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 可以理解为私钥的"构造函数"
|
||||
* 返回的数据类型为PrivateKeyInterface接口, 必要时调用者需要手动将类型强制转化为需要的类型
|
||||
* @param bytesOfPrivateKey 私钥的bytes数组
|
||||
* @param algorithm 私钥类型
|
||||
* @return minsecurity.crypto.PrivateKeyInterface
|
||||
* @throws
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static PrivateKeyInterface unMarshalPrivateKey(byte[] bytesOfPrivateKey, int algorithm){
|
||||
if(bytesOfPrivateKey == null || bytesOfPrivateKey.length == 0){
|
||||
return null;
|
||||
@@ -48,6 +58,14 @@ public class KeyUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个16字节的byte数组, 可用作sm4的key
|
||||
* @param passwd 密码
|
||||
* @return byte[]
|
||||
* @throws
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static byte[] get16bytePasswd(byte[] passwd) {
|
||||
byte[] hashPasswd = HashAlgo.sm3(passwd);
|
||||
if( hashPasswd.length == 32){
|
||||
|
||||
@@ -29,6 +29,14 @@ public class SM4 {
|
||||
*/
|
||||
public static final int DEFAULT_KEY_SIZE = 128;
|
||||
|
||||
/**
|
||||
* 生成16字节的sm4 key
|
||||
* @param
|
||||
* @return byte[]
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static byte[] generateKey() throws NoSuchAlgorithmException, NoSuchProviderException {
|
||||
return generateKey(DEFAULT_KEY_SIZE);
|
||||
}
|
||||
|
||||
@@ -165,7 +165,14 @@ public class SM2Base {
|
||||
parameterSpec.getN(), parameterSpec.getH());
|
||||
return new ECPublicKeyParameters(ecPubKey.getQ(), domainParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一对sm2公私钥
|
||||
* @param
|
||||
* @return minsecurity.crypto.sm2.SM2KeyPair
|
||||
* @throws
|
||||
* @author hongyu guo
|
||||
* @date 2021/3/15
|
||||
**/
|
||||
public static SM2KeyPair generateSM2KeyPair(){
|
||||
return SM2KeyPair.generateKeyPair();
|
||||
}
|
||||
|
||||
@@ -336,8 +336,8 @@ public class Identity {
|
||||
"Name='" + Name + '\'' +
|
||||
", KeyParam.signAlgo=" + KeyParam.SignatureAlgorithm +
|
||||
", KeyParam.pubAlgo=" + KeyParam.PublicKeyAlgorithm +
|
||||
", Prikey=" + ByteUtils.toHexString(Prikey.getBytes()) +
|
||||
", pubKey=" + ByteUtils.toHexString(Pubkey.getBytes()) +
|
||||
", Prikey=" + (Prikey != null ? ByteUtils.toHexString(Prikey.getBytes()) : "null") +
|
||||
", pubKey=" + (Pubkey != null ? ByteUtils.toHexString(Pubkey.getBytes()) : "null") +
|
||||
", PrikeyRawByte=" + (PrikeyRawByte == null ? "null":ByteUtils.toHexString(PrikeyRawByte)) +
|
||||
", Pubkey=" + Pubkey +
|
||||
", Passwd='" + Passwd + '\'' +
|
||||
|
||||
@@ -81,7 +81,8 @@ public class InnerIdentity {
|
||||
public String toString() {
|
||||
return "InnerIdentity{" +
|
||||
"Name='" + Name + '\'' +
|
||||
", KeyParam=" + KeyParam +
|
||||
", KeyParam.PublicKeyAlgorithm=" + KeyParam.PublicKeyAlgorithm +
|
||||
", KeyParam.SignatureAlgorithm=" + KeyParam.SignatureAlgorithm +
|
||||
", Prikey=" + (Prikey == null ? "null" : ByteUtils.toHexString(Prikey)) +
|
||||
", Pubkey=" + (Pubkey == null ? "null" : ByteUtils.toHexString(Pubkey)) +
|
||||
", Passwd='" + Passwd + '\'' +
|
||||
|
||||
@@ -2,6 +2,10 @@ package util;
|
||||
|
||||
|
||||
import encoding.VlInt;
|
||||
import encoding.VlIntException;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Arrays;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -18,7 +22,9 @@ public class ByteHelper {
|
||||
* @param startIndex
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getTailBytes(byte[] buffer,int startIndex){
|
||||
public static byte[] getTailBytes(byte[] buffer,int startIndex) throws ByteHelperException {
|
||||
if(startIndex >= buffer.length || startIndex < 0)
|
||||
throw new ByteHelperException("buffer out of bounds");
|
||||
int len=buffer.length-startIndex;
|
||||
byte[] newBytes=new byte[len];
|
||||
System.arraycopy(buffer,startIndex,newBytes,0,len);
|
||||
@@ -27,16 +33,25 @@ public class ByteHelper {
|
||||
|
||||
/**
|
||||
* 从byte[]数组指定开始位置获取第一个字节
|
||||
* todo: start位置可能是大于int类型的数值
|
||||
*
|
||||
* @param buffer
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static byte getFirstByte(byte[] buffer, VlInt start){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
byte[] firstbyte=new byte[1];
|
||||
System.arraycopy(buffer,startInt,firstbyte,0,firstbyte.length);
|
||||
return firstbyte[0];
|
||||
public static byte getFirstByte(byte[] buffer, VlInt start) throws ByteHelperException {
|
||||
int startInt= 0;
|
||||
try {
|
||||
startInt = start.getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(startInt >= buffer.length || startInt < 0){
|
||||
throw new ByteHelperException("buffer out of bounds");
|
||||
}
|
||||
return buffer[startInt];
|
||||
// byte[] firstbyte=new byte[1];
|
||||
// System.arraycopy(buffer,startInt,firstbyte,0,firstbyte.length);
|
||||
// return firstbyte[0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,16 +61,33 @@ public class ByteHelper {
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
int lenInt=len.getVlIntValue2Int();
|
||||
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len) throws ByteHelperException {
|
||||
int startInt= -1;
|
||||
int lenInt = -1;
|
||||
try {
|
||||
startInt = start.getVlIntValue2Int();
|
||||
lenInt=len.getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(startInt >= buffer.length || startInt < 0){
|
||||
throw new ByteHelperException("buffer out of bounds");
|
||||
}
|
||||
if(lenInt < 0){
|
||||
throw new ByteHelperException("len should > 0");
|
||||
}
|
||||
byte[] lenbytes=new byte[lenInt];
|
||||
// byte[] lenbytes=new byte[1];
|
||||
System.arraycopy(buffer,startInt,lenbytes,0,lenInt);
|
||||
return lenbytes;
|
||||
}
|
||||
public static byte[] getLenBytes(byte[] buffer, VlInt start,int len){
|
||||
int startInt=start.getVlIntValue2Int();
|
||||
int startInt= -1;
|
||||
try {
|
||||
startInt = start.getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
byte[] lenbytes=new byte[len];
|
||||
// byte[] lenbytes=new byte[1];
|
||||
System.arraycopy(buffer,startInt,lenbytes,0,len);
|
||||
@@ -118,11 +150,11 @@ public class ByteHelper {
|
||||
if(bytes.length!=8){
|
||||
return Integer.parseInt(null);
|
||||
}
|
||||
int value= 0;
|
||||
long value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
|
||||
value +=((long)bytes[i] & 0xff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -217,6 +249,7 @@ public class ByteHelper {
|
||||
bytes[i] = 0x00;
|
||||
}
|
||||
System.arraycopy(uint32,0,bytes,4,uint32.length);
|
||||
// System.out.println(Arrays.toString(uint32) + "uint32ToLong: " + Arrays.toString(bytes));
|
||||
return byteArrayToLong(bytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package util;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:38 2021/03/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class ByteHelperException extends Exception{
|
||||
public ByteHelperException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user