mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
fix conflict
This commit is contained in:
commit
c5e2af1b27
29 files changed
+314
-81
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;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package component;
|
||||
|
||||
import encoding.*;
|
||||
import minsecurity.Common;
|
||||
import org.omg.CORBA.PRIVATE_MEMBER;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -44,7 +43,6 @@ class SignatureAlgorithm{
|
||||
}
|
||||
}
|
||||
public class SignatureType implements TlvComponentBase, IEncodingAble {
|
||||
// private SignatureAlgorithm value;
|
||||
private SignatureAlgorithm value; // SignatureAlgorithm
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -94,7 +94,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);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import static org.junit.Assert.*;
|
||||
public class BlockTest {
|
||||
|
||||
@Test
|
||||
public void TestCreateBlockByTypeLengthBuffer() {
|
||||
public void TestCreateBlockByTypeLengthBuffer() throws VlIntException {
|
||||
byte[] buf = new byte[10000];
|
||||
Block block = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
|
||||
//测试type与size
|
||||
@@ -42,7 +42,7 @@ public class BlockTest {
|
||||
|
||||
//error:block.Size().size()错误。
|
||||
@Test
|
||||
public void TestCreateBlockByBuffer() {
|
||||
public void TestCreateBlockByBuffer() throws VlIntException {
|
||||
byte[] buf = new byte[10000];
|
||||
buf[0] = (byte)212;
|
||||
buf[1] = (byte)241;
|
||||
@@ -59,7 +59,7 @@ public class BlockTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestBuildByBlock(){
|
||||
public void TestBuildByBlock() throws VlIntException {
|
||||
//Block构造方法测试
|
||||
byte[] buf = new byte[10000];
|
||||
Block block1 = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
|
||||
@@ -85,7 +85,7 @@ public class BlockTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void TestBuildByTypeLengthBuffer(){
|
||||
public void TestBuildByTypeLengthBuffer() throws VlIntException {
|
||||
//Block构造方法测试
|
||||
byte[] buf = new byte[10000];
|
||||
Block block = new Block(new VlInt(241),new VlInt(8000),ByteHelper.getLenBytes(buf,new VlInt(0),8000),true);
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package encoding;
|
||||
|
||||
|
||||
import minsecurity.crypto.TestSM2;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
@@ -17,6 +19,8 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
|
||||
public class VlIntTest {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(VlIntTest.class);
|
||||
|
||||
|
||||
// @Test
|
||||
// public void size() {
|
||||
@@ -41,7 +45,7 @@ public class VlIntTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getVlIntValue2Int() {
|
||||
public void getVlIntValue2Int() throws VlIntException {
|
||||
//vlint(int)的size小于4时,测试是否能准确转为int输出
|
||||
int a = 222;
|
||||
int a2 = 22222222;
|
||||
@@ -66,18 +70,25 @@ public class VlIntTest {
|
||||
|
||||
//error:以bytes[]形式构造的VlInt,2^31-1 到 2^32-1之间的数会出错
|
||||
@Test
|
||||
public void getVlIntValue2Long() {
|
||||
public void getVlIntValue2Long() throws VlIntException {
|
||||
long l1 = new Long(0);
|
||||
long l2 = new Long(1);
|
||||
long l3 = new Long(4294967295L); //2^32-1
|
||||
byte[] b1 = new byte[]{0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2^31-1
|
||||
byte[] b2= new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31
|
||||
|
||||
byte[] b2 = new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31
|
||||
byte[] b3 = new byte[]{0,0,0,0,-128,0,0,0};
|
||||
BigInteger bigInteger = new BigInteger(b2);
|
||||
System.out.println(bigInteger.longValue());
|
||||
System.out.println(new BigInteger(b3).longValue());
|
||||
VlInt vlInt_long1 = new VlInt(l1); // size = 1
|
||||
VlInt vlInt_long2 = new VlInt(l2); // size = 1
|
||||
VlInt vlInt_long3 = new VlInt(l3); // size = 5
|
||||
VlInt vlInt_long4 = new VlInt(b1);
|
||||
VlInt vlInt_long5 = new VlInt(b2);
|
||||
System.out.println(vlInt_long2.toString());
|
||||
System.out.println(vlInt_long3.toString());
|
||||
System.out.println(vlInt_long4.toString());
|
||||
System.out.println(vlInt_long5.toString());
|
||||
//以VlInt(long)形式构造的VlInt
|
||||
assertEquals(vlInt_long1.getVlIntValue2Long(),l1);
|
||||
assertEquals(vlInt_long2.getVlIntValue2Long(),l2);
|
||||
@@ -85,6 +96,12 @@ public class VlIntTest {
|
||||
///以VlInt(bytes[])形式构造的VlInt
|
||||
assertEquals(vlInt_long4.getVlIntValue2Long(),2147483647L);//2^31-1
|
||||
assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31
|
||||
|
||||
|
||||
assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31
|
||||
byte[] bytes1 = new byte[]{127,-128,-128,-128,-128,-128,-128,-128};
|
||||
VlInt vlInt_6 = new VlInt(new BigInteger("21474836481"));
|
||||
assertEquals(vlInt_6.getVlIntValue2Long(), new BigInteger("21474836481").longValue());
|
||||
}
|
||||
|
||||
//error:以bytes[]形式构造的VlInt,在大于2^31-1的数会出错
|
||||
@@ -157,21 +174,25 @@ public class VlIntTest {
|
||||
int a1 = 0,a2 = 65535;
|
||||
long l = 4294967296L;
|
||||
int s1 = a2-a1,s2 = a1-a2;
|
||||
// long r1 = l-a1,r2 = l-a2; //差值越界int最大值
|
||||
long r1 = l-a1,r2 = l - (long)a2; //差值越界int最大值
|
||||
VlInt vlInt1 = new VlInt(a1);
|
||||
VlInt vlInt2 = new VlInt(a2);
|
||||
VlInt vlInt3 = new VlInt(l);
|
||||
logger.debug(vlInt1.toString());
|
||||
logger.debug(vlInt2.toString());
|
||||
logger.debug(vlInt3.toString());
|
||||
|
||||
assertEquals(vlInt2.subtract(vlInt1),s1);
|
||||
assertEquals(vlInt1.subtract(vlInt2),s2); //差为负数
|
||||
//error:差值越界int最大值
|
||||
// assertEquals(vlInt3.subtract(vlInt2),r1);
|
||||
assertEquals(vlInt3.subtract(vlInt2), r2);
|
||||
|
||||
|
||||
int sum1 = a1+a2;
|
||||
long sum2 = a2+l;
|
||||
//param:VlInt
|
||||
assertEquals(vlInt1.add(vlInt2),sum1);
|
||||
// assertEquals(vlInt1.add(vlInt3),sum2); //两者之和为long,越界int最大值
|
||||
assertEquals(vlInt2.add(vlInt3),sum2); //两者之和为long,越界int最大值
|
||||
//param:int
|
||||
assertEquals(vlInt1.add(a2),sum1);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ public class TestCert {
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
@@ -94,7 +93,7 @@ public class TestCert {
|
||||
|
||||
String pem2 = CertUtils.toPem(certificate, new byte[]{1,2,3,4,5,6,7,8},Common.SM4ECB);
|
||||
Certificate certFromPem2 = CertUtils.fromPem(pem2, new byte[]{1,2,3,4,5,6,7,8},Common.SM4ECB);
|
||||
logger.debug(certFromPem.toString());
|
||||
logger.debug(certFromPem2.toString());
|
||||
assertTrue(CertUtils.verifyCert(certFromPem2,certFromPem2));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import minsecurity.crypto.sm2.SM2KeyPair;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 17:22 2021/03/15
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class testPrivate {
|
||||
private void func1(){
|
||||
System.out.println("private func1");
|
||||
}
|
||||
|
||||
private void func2(int a, SM2KeyPair sm2KeyPair){
|
||||
System.out.println("a = " + a);
|
||||
System.out.println("private key = " + ByteUtils.toHexString(sm2KeyPair.getSm2PrivateKey().getBytes()));
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,7 @@ import static org.junit.Assert.*;
|
||||
*/public class ByteHelperTest {
|
||||
|
||||
@Test
|
||||
public void getTailBytes(){
|
||||
ByteHelper byteHelper = new ByteHelper();
|
||||
public void getTailBytes() throws ByteHelperException {
|
||||
//偶数字节
|
||||
byte[] buffer = new byte[]{0x00,0x01,(byte) 0x80,0x7f}; //1
|
||||
byte[] test1 = new byte[]{0x01,(byte) 0x80,0x7f};
|
||||
@@ -23,16 +22,15 @@ import static org.junit.Assert.*;
|
||||
byte[] buffer2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
byte[] test2 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,(byte) 0xff, (byte) 0xff}; //65535
|
||||
int startIndex = 1;
|
||||
byte[] b = byteHelper.getTailBytes(buffer,startIndex);
|
||||
byte[] b2 = byteHelper.getTailBytes(buffer2,startIndex);
|
||||
byte[] b = ByteHelper.getTailBytes(buffer,startIndex);
|
||||
byte[] b2 = ByteHelper.getTailBytes(buffer2,startIndex);
|
||||
assertArrayEquals(b,test1);
|
||||
assertArrayEquals(b2,test2);
|
||||
}
|
||||
|
||||
//todo:对start值进行限定,应当小于Byte数组的length
|
||||
@Test
|
||||
public void getFirstByte() {
|
||||
byte[] b0 = new byte[]{0x00,0x00,0x01};
|
||||
public void getFirstByte() throws ByteHelperException {
|
||||
byte[] b0 = new byte[]{0x00,0x00,1};
|
||||
byte[] test = new byte[]{0x7f,0x7f,0x7f,0x7f};
|
||||
VlInt start0 = new VlInt(b0);
|
||||
byte firstbyte = ByteHelper.getFirstByte(test,start0);
|
||||
@@ -41,7 +39,7 @@ import static org.junit.Assert.*;
|
||||
|
||||
|
||||
@Test
|
||||
public void getLenBytes() {
|
||||
public void getLenBytes() throws ByteHelperException {
|
||||
byte[] buffer = new byte[]{0x00,0x00,0x01, (byte) 0xff}; //255
|
||||
byte[] test = new byte[]{0x00,0x00,0x01};
|
||||
VlInt start = new VlInt(0);
|
||||
|
||||
Reference in new issue
Block a user