Merge remote-tracking branch 'origin/master'

This commit is contained in:
ChessNineeee committed 2021-04-01 22:06:20 +08:00
commit 33b3d40855
17 files changed
+614 -246

No files matched your search

@@ -353,9 +353,8 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
} else if(isByteArray()){
return StringHelper.escape(new String(this.byteArrayValue));
}else{
// return StringHelper.FormatUint(this.intValue,10);
// todo: 需要对long类型按照指定编码规则编码成字符串
return "";
return Long.valueOf(this.intValue).toString();
}
}
}
+35 -39
View File
@@ -13,10 +13,9 @@ import encoding.*;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class KeyLocator implements TlvComponentBase, IEncodingAble {
private Identifier identifier;
private Identifier identifier=new Identifier();
public KeyLocator(){
identifier=new Identifier();
}
/**
@@ -24,15 +23,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
* @param block
*/
public KeyLocator(Block block) throws ComponentException {
this.buildKeyLocatorByBlock(block);
if(!this.buildKeyLocatorByBlock(block)){
throw new ComponentException("KeyLocator(Block): block invalid.");
}
}
/**
* 根据标识字符串 创建一个 KeyLocator
* @param identifierString
*/
public KeyLocator(String identifierString){
this.buildKeyLocatorByString(identifierString);
public KeyLocator(String identifierString) throws ComponentException {
if(!this.buildKeyLocatorByString(identifierString)){
throw new ComponentException("KeyLocator(String): string invalid.");
}
}
/**
@@ -110,42 +113,33 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = this.identifier.wireEncode(encoder);
} catch (ComponentException e) {
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = this.identifier.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvKeyLocator));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (ComponentException | EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("KeyLocator.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -157,17 +151,19 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
public boolean wireDecode(Block block) throws ComponentException {
try {
// 检查Type是否正确
if(!TLV.isValidIdentifierType(block.getType())){
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvKeyLocator))){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
return false;
}
// 解析子TLV为一个Identifier
return this.identifier.wireDecode(block.getSubElements().getBlock(0));
} catch (BlockException e) {
+19 -16
View File
@@ -10,25 +10,12 @@ import minsecurity.identity.Identity;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Signature {
public SignatureInfo getSigInfo() {
return sigInfo;
}
private SignatureInfo sigInfo=new SignatureInfo();
private SignatureValue sigValue=new SignatureValue();
public void setSigInfo(SignatureInfo sigInfo) {
this.sigInfo = sigInfo;
public Signature(){
}
public SignatureValue getSigValue() {
return sigValue;
}
public void setSigValue(SignatureValue sigValue) {
this.sigValue = sigValue;
}
private SignatureInfo sigInfo;
private SignatureValue sigValue;
/**
* 根据网络身份创建一个 Signature
* @param identity
@@ -46,4 +33,20 @@ public class Signature {
this.sigInfo=signatureInfo;
this.sigValue=signatureValue;
}
public SignatureInfo getSigInfo() {
return sigInfo;
}
public void setSigInfo(SignatureInfo sigInfo) {
this.sigInfo = sigInfo;
}
public SignatureValue getSigValue() {
return sigValue;
}
public void setSigValue(SignatureValue sigValue) {
this.sigValue = sigValue;
}
}
+76 -95
View File
@@ -14,22 +14,20 @@ import encoding.*;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureInfo implements TlvComponentBase, IEncodingAble {
private SignatureType signatureType;
private KeyLocator keyLocator;
private SignatureType signatureType=new SignatureType();
private KeyLocator keyLocator=new KeyLocator();
public SignatureInfo(){
signatureType=new SignatureType();
keyLocator=new KeyLocator();
}
/**
* 根据签名值类型创建一个 SignatureInfo
* @param value
*/
public SignatureInfo(SignatureAlgorithm value){
signatureType=new SignatureType();
keyLocator=new KeyLocator();
this.buildSignatureInfoBySignatureTypeValue(value);
public SignatureInfo(SignatureAlgorithm value) throws ComponentException {
if(!this.buildSignatureInfoBySignatureTypeValue(value)){
throw new ComponentException("SignatureInfo(SignatureAlgorithm value): value invalid");
}
}
/**
@@ -38,7 +36,10 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
* @param locator
*/
public SignatureInfo(SignatureAlgorithm value,KeyLocator locator) throws ComponentException {
this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator);
if(!this.buildSignatureInfoBySignatureTypeValueAndKeyLocator(value,locator)){
throw new ComponentException("SignatureInfo(SignatureAlgorithm value,KeyLocator locator):" +
" value or locator invalid");
}
}
/**
@@ -69,7 +70,7 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
return false;
}
if(!this.keyLocator.buildKeyLocatorByIdentifier(keyLocator.getIdentifier())){
if(!this.keyLocator.buildKeyLocatorByIdentifier(locator.getIdentifier())){
return false;
}
@@ -108,51 +109,41 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
if(!this.keyLocator.isValid()){
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen= 0;
try {
tmplen = this.keyLocator.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
try {
int totalLength = 0;
// 编码 TLV-VALUE
if (this.keyLocator.isValid()) {
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen = this.keyLocator.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
}
if(tmplen<=0){
int tmplen = this.signatureType.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
}
int tmplen= 0;
tmplen = this.signatureType.wireEncode(encoder);
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
} catch (EncoderException e) {
throw new ComponentException("SignatureInfo.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -162,57 +153,47 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureInfo))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureInfo))) {
return false;
}
} catch (BlockException e) {
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
boolean findSignatureType = false;
int elementSize = block.getSubElements().length();
for (int i = 0; i < elementSize; i++) {
Block cacheBlock = block.getSubElements().getBlock(i);
int type = cacheBlock.getType().getVlIntValue2Int();
switch (type) {
case TLV.TlvSignatureType:
if (!this.signatureType.wireDecode(cacheBlock)) {
return false;
}
findSignatureType = true;
break;
case TLV.TlvKeyLocator:
if (!this.keyLocator.wireDecode(cacheBlock)) {
return false;
}
break;
default:
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
}
}
// 缺失必要的 SignatureType 子TLV
if (!findSignatureType) {
throw new Error("SignatureInfo.wireDecode: Parse SignatureInfo error, require SignatureType sub-element");
}
return true;
} catch (BlockException | VlIntException e) {
throw new ComponentException("SignatureInfo.wireDecode: "+e.getMessage());
}
boolean findSignatureType=false;
int elementSize=block.getSubElements().length();
for (int i=0;i<elementSize;i++) {
Block cacheBlock= null;
try {
cacheBlock = block.getSubElements().getBlock(i);
} catch (BlockException e) {
throw new ComponentException("SignatureInfo.wireDecode:"+e.getMessage());
}
int type= 0;
try {
type = cacheBlock.getType().getVlIntValue2Int();
} catch (VlIntException e) {
e.printStackTrace();
}
switch (type){
case TLV.TlvSignatureType:
if(!this.signatureType.wireDecode(cacheBlock)){
return false;
}
findSignatureType=true;
case TLV.TlvKeyLocator:
if(!this.keyLocator.wireDecode(cacheBlock)){
return false;
}
default:
throw new ComponentException("SignatureInfo.wireDecode: SignatureInfo sub element type error");
// return false;
}
}
// 缺失必要的 SignatureType 子TLV
if(!findSignatureType){
throw new Error("SignatureInfo.wireDecode: Parse SignatureInfo error, require SignatureType sub-element");
}
return true;
}
}
+38 -47
View File
@@ -108,43 +108,35 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
} catch (EncoderException e) {
throw new ComponentException("SignatureType.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -154,23 +146,22 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureType))){
return false;
}
// 尝试从 TLE-VALUE 中读取出一个非负整数
long result= 0;
try {
result = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureType))) {
return false;
}
// 尝试从 TLE-VALUE 中读取出一个非负整数
long result = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
if (result < 0) {
return false;
}
this.value = new SignatureAlgorithm((int) result);
return true;
} catch (TLVException | VlIntException e) {
throw new ComponentException("SignatureType.wireDecode: " + e.getMessage());
}
if(result<0){
return false;
}
this.value=new SignatureAlgorithm((int)result);
return true;
}
}
+42 -45
View File
@@ -15,12 +15,17 @@ import encoding.*;
public class SignatureValue implements TlvComponentBase, IEncodingAble {
private byte[] value;
public SignatureValue(){
}
/**
* 根据签名值字节数组创建一个 SignatureValue
* @param value
*/
public SignatureValue(byte[] value){
this.buildSignatureValueByByteArray(value);
public SignatureValue(byte[] value) throws ComponentException {
if(!this.buildSignatureValueByByteArray(value)){
throw new ComponentException("Signature(byte[] value): value invalid");
}
}
/**
@@ -28,7 +33,9 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
* @param block
*/
public SignatureValue(Block block) throws ComponentException {
this.buildSignatureValueByBlock(block);
if(!this.buildSignatureValueByBlock(block)){
throw new ComponentException("Signature(Block block): block invalid");
}
}
/**
@@ -66,42 +73,33 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependByteArray(this.value,new SizeT(this.value.length));
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
} catch (EncoderException e) {
throw new ComponentException("SignatureValue.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -111,19 +109,18 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureValue))){
return false;
}
int len= 0;
try {
len = block.getLength().getVlIntValue2Int();
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureValue))) {
return false;
}
int len = block.getLength().getVlIntValue2Int();
this.value = new byte[len];
System.arraycopy(block.getValue(), 0, this.value, 0, len);
return true;
} catch (VlIntException e) {
throw new ComponentException("SignatureValue.wireDecode: "+e.getMessage());
}
this.value=new byte[len];
System.arraycopy(block.getValue(),0,this.value,0,len);
return true;
}
}
-1
View File
@@ -247,7 +247,6 @@ public class Block {
if ((this.value.length == 0) || this.elements.length() != 0) {
return true;
}
// 循环解析所有的一级子TLV
for (int start = 0; start < this.value.length; ) {
// 读取 TLV-TYPE
@@ -19,7 +19,7 @@ import java.nio.charset.StandardCharsets;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class HashAlgo {
// TODO: 单元测试 基本上只需要对sm3做单元测试
///////////////
///// SM3 /////
///////////////
+2
View File
@@ -212,10 +212,12 @@ public class Data implements IEncodingAble, InteractWithField {
if (!this.freshnessPeriod.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvPayload:
if (!this.payload.wireDecode(newblock)) {
return false;
}
break;
default:
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
+6
View File
@@ -258,26 +258,32 @@ public class Interest implements InteractWithField, IEncodingAble {
if (!this.canBePrefix.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvMustBeRefresh:
if (!this.mustBeRefresh.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvInterestLifeTime:
if (!this.interestLifeTime.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvNonce:
if (!this.nonce.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvHopLimit:
if (!this.hopLimit.wireDecode(newblock)) {
return false;
}
break;
case TLV.TlvPayload:
if (!this.payload.wireDecode(newblock)) {
return false;
}
break;
default:
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
+4
View File
@@ -147,21 +147,25 @@ public class MINPacket implements IEncodingAble {
// 标示解析到了至少一个以上的合法标识
existIdentifier = true;
break;
case TLV.TlvSignatureField: // 签名区
// 解析签名区,保证如果存在签名,则签名均符合格式
if (!this.signatureField.wireDecode(cacheBlock)) {
return false;
}
break;
case TLV.TlvReadOnlyField: // 只读区
// 解析只读区
if (!this.readOnlyField.wireDecode(cacheBlock)) {
return false;
}
break;
case TLV.TlvMutableField: // 可变区
// 解析可变区
if (!this.mutableField.wireDecode(cacheBlock)) {
return false;
}
break;
default:
return false;
+100
View File
@@ -0,0 +1,100 @@
package component;
import encoding.*;
import org.checkerframework.checker.units.qual.K;
import org.junit.Test;
import packet.PacketException;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 19:08 2021/3/31
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class KeyLocatorTest {
@Test
public void TestCreateKeyLocatorByString() throws ComponentException {
String str="/install/hello/王";
KeyLocator got=new KeyLocator(str);
System.out.println("keyLocator: "+got.getIdentifier().toUri());
}
@Test
public void TestCreateKeyLocatorByBlock() throws EncoderException, ComponentException, BlockException {
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
String str="/min/王/123/哈哈哈哈";
IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str);
System.out.println("type: "+got.getTlvType());
got.wireEncode(encoder);
byte[] rawBuf=encoder.getBuffer();
System.out.println("buf: "+ Arrays.toString(rawBuf));
// rawBuf[4]=101;
byte[] buf2=new byte[rawBuf.length-2];
System.arraycopy(rawBuf,2,buf2,0,rawBuf.length-2);
System.out.println("buf2: "+ Arrays.toString(buf2));
Block block=new Block(new VlInt(203),new VlInt(rawBuf.length-2),buf2,true);
block.parseSubElements();
System.out.println(block.getSubElements().getBlock(0).getType());
// System.out.println(block.getSubElements().getBlock(1).getType());
KeyLocator gotKey=new KeyLocator(block);
System.out.println(gotKey.getIdentifier().toUri());
System.out.println(gotKey.isValid());
}
@Test
public void TestCreateKeyLocatorByComponents() throws ComponentException {
IdentifierComponent component=new IdentifierComponent("min");
IdentifierComponent component1=new IdentifierComponent("pku");
IdentifierComponent component2=new IdentifierComponent("王");
IdentifierComponentContainer gotComponent=new IdentifierComponentContainer();
gotComponent.addElement(component);
gotComponent.addElement(component1);
gotComponent.addElement(component2);
KeyLocator gotKey=new KeyLocator(gotComponent);
System.out.println(gotKey.getIdentifier().toUri());
}
@Test
public void TestCreateKeyLocatorByIdentifier() throws ComponentException, EncoderException {
Identifier gotIdentifier=new Identifier("/min/pku/111");
KeyLocator gotKey=new KeyLocator(gotIdentifier);
System.out.println(gotKey.getIdentifier().toUri());
IdentifierComponent component=new IdentifierComponent("min");
IdentifierComponent component1=new IdentifierComponent("pku");
IdentifierComponent component2=new IdentifierComponent(111);
IdentifierComponentContainer gotComponent=new IdentifierComponentContainer();
gotComponent.addElement(component);
gotComponent.addElement(component1);
gotComponent.addElement(component2);
KeyLocator gotKey1=new KeyLocator(gotComponent);
System.out.println(gotKey1.getIdentifier().toUri());
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
gotKey.wireEncode(encoder);
System.out.println(Arrays.toString(encoder.getBuffer()));
Encoder encoder1=new Encoder();
encoder1.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
gotKey1.wireEncode(encoder1);
System.out.println(Arrays.toString(encoder1.getBuffer()));
}
@Test
public void TestKeyLocator_WireDecode() throws ComponentException, BlockException, EncoderException, PacketException {
Identifier gotIdentifier=new Identifier("/min/pku/111");
KeyLocator gotKey=new KeyLocator(gotIdentifier);
System.out.println(gotKey.getIdentifier().toUri());
Block block=new SelfEncodingBase().selfWireEncode(gotKey);
KeyLocator newK=new KeyLocator();
newK.wireDecode(block);
System.out.println(newK.getIdentifier().toUri());
}
}
@@ -0,0 +1,93 @@
package component;
import encoding.*;
import org.checkerframework.checker.units.qual.A;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 20:27 2021/3/31
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureInfoTest {
@Test
public void TestCrateSignatureInfoBySignatureTypeValue() throws ComponentException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo get=new SignatureInfo(value);
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
}
@Test
public void TestCreateSignatureInfoByBlock() throws ComponentException, EncoderException, BlockException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo get=new SignatureInfo(value);
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("get buf size: "+get.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println(Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println(block.parseSubElements());
System.out.println("subElem: "+block.getSubElements().length());
SignatureInfo got=new SignatureInfo(block);
got.wireDecode(block);
System.out.println("sigAlgo: "+got.getSignatureType().getValue().getSignatureAlgorithm());
System.out.println("uri: "+got.getKeyLocator().getIdentifier().toUri());
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("got buf size: "+got.wireEncode(encoder));
System.out.println(Arrays.toString(encoder.getBuffer()));
}
@Test
public void TestSignatureInfo_WireDecodeWithValue() throws ComponentException, EncoderException, BlockException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo get=new SignatureInfo(value);
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("get buf size: "+get.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println(Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println(block.parseSubElements());
System.out.println("subElem: "+block.getSubElements().length());
for (int i = 0; i < block.getSubElements().length(); i++) {
System.out.println(block.getSubElements().getBlock(i).getType());
System.out.println(Arrays.toString(block.getSubElements().getBlock(i).getValue()));
}
System.out.println(get.getSignatureType().getValue().getSignatureAlgorithm());
}
@Test
public void TestSignatureInfo_WireDecodeWithLocator() throws ComponentException, EncoderException, BlockException {
KeyLocator keyLocator=new KeyLocator("/mnfdinstall");
System.out.println(keyLocator.getIdentifier().toUri());
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo get=new SignatureInfo(value,keyLocator);
System.out.println("algo: "+get.getSignatureType().getValue().getSignatureAlgorithm());
System.out.println(get.getKeyLocator().getIdentifier().toUri());
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
System.out.println("get buf size: "+get.wireEncode(encoder));
byte[] buf=encoder.getBuffer();
System.out.println("buf: "+Arrays.toString(buf));
Block block=new Block(buf,false);
System.out.println(block.parseSubElements());
System.out.println("subElem: "+block.getSubElements().length());
SignatureInfo newSig=new SignatureInfo();
newSig.wireDecode(block);
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
newSig.wireEncode(encoder);
System.out.println("new buf: "+ Arrays.toString(encoder.getBuffer()));
System.out.println("new algo: "+newSig.getSignatureType().getValue().getSignatureAlgorithm());
System.out.println("new uri: "+newSig.getKeyLocator().getIdentifier().toUri());
}
}
@@ -0,0 +1,21 @@
package component;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 20:52 2021/4/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureTest {
@Test
public void testSignature(){
Signature signature=new Signature();
System.out.println(signature.getSigInfo().getSignatureType().getValue().getSignatureAlgorithm());
System.out.println(Arrays.toString(signature.getSigValue().getValue()));
}
}
@@ -0,0 +1,76 @@
package component;
import encoding.*;
import org.junit.Test;
import packet.PacketException;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 10:48 2021/3/31
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureTypeTest {
@Test
public void TestCreateSignatureTypeByBlock() throws EncoderException, ComponentException, BlockException {
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
String str="/min/pkusz";
IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str);
System.out.println("type: "+got.getTlvType().toString());
got.wireEncode(encoder);
byte[] rawBuf=encoder.getBuffer();
System.out.println("buf: "+ Arrays.toString(rawBuf));
byte[] buf2=new byte[4];
System.arraycopy(rawBuf,14,buf2,0,4);
Block block=new Block(new VlInt(202),new VlInt(4),buf2,true);
System.out.println("block length: "+block.getLength());
SignatureType gotContent=new SignatureType(block);
System.out.println("sigtype: "+gotContent.getValue().getSignatureAlgorithm());
}
@Test
public void TestCreateSignatureTypeByValue() throws PacketException, EncoderException, BlockException, ComponentException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureType got=new SignatureType(value);
System.out.println(got.getValue().getSignatureAlgorithm());
Block rawBlock=new SelfEncodingBase().selfWireEncode(got);
System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue()));
}
@Test
public void TestSignatureType_WireEncode() throws PacketException, EncoderException, BlockException, ComponentException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureType got=new SignatureType(value);
System.out.println(got.getValue().getSignatureAlgorithm());
Block rawBlock=new SelfEncodingBase().selfWireEncode(got);
System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue()));
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
got.wireEncode(encoder);
byte[] gotBuf=encoder.getBuffer();
System.out.println(Arrays.toString(gotBuf));
}
@Test
public void TestCreateSignatureTypeByValue2() throws PacketException, EncoderException, BlockException, ComponentException {
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureType got=new SignatureType(value);
System.out.println(got.getValue().getSignatureAlgorithm());
Block rawBlock=new SelfEncodingBase().selfWireEncode(got);
System.out.println(rawBlock.getType()+" "+rawBlock.getLength()+" "+Arrays.toString(rawBlock.getValue()));
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
got.wireEncode(encoder);
byte[] gotBuf=encoder.getBuffer();
System.out.println(Arrays.toString(gotBuf));
SignatureType newSig=new SignatureType();
newSig.wireDecode(rawBlock);
System.out.println(newSig.getValue().getSignatureAlgorithm());
}
}
@@ -0,0 +1,69 @@
package component;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 20:21 2021/4/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureValueTest {
@Test
public void TestCreateSignatureValueByByteArray() throws ComponentException {
byte[] buf={(byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8};
SignatureValue got=new SignatureValue(buf);
System.out.println(Arrays.toString(got.getValue()));
}
@Test
public void TestCreateSignatureValueByBlock() throws EncoderException, ComponentException, BlockException {
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
String str="/min/pku";
IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str);
System.out.println("type: "+got.getTlvType());
got.wireEncode(encoder);
byte[] rawBuf=encoder.getBuffer();
System.out.println("buf: "+Arrays.toString(rawBuf));
byte[] a=new byte[4];
System.arraycopy(rawBuf,12,a,0,4);
Block block=new Block(new VlInt(200),new VlInt(4),a,true);
SignatureValue gotValue=new SignatureValue(block);
System.out.println("sv value: "+Arrays.toString(gotValue.getValue()));
}
@Test
public void TestSignatureValue_WireEncode() throws BlockException, ComponentException, EncoderException {
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
String str="/min/pku";
IdentifierWrapper got=new IdentifierWrapper().createCommonIdentifierByString(str);
System.out.println("type: "+got.getTlvType());
got.wireEncode(encoder);
byte[] rawBuf=encoder.getBuffer();
System.out.println("buf: "+Arrays.toString(rawBuf));
byte[] a=new byte[4];
System.arraycopy(rawBuf,12,a,0,4);
Block block=new Block(new VlInt(200),new VlInt(4),a,true);
SignatureValue gotValue=new SignatureValue(block);
System.out.println("sv value: "+Arrays.toString(gotValue.getValue()));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
gotValue.wireEncode(encoder);
System.out.println("all: "+Arrays.toString(encoder.getBuffer()));
byte[] bytes=encoder.getBuffer();
Block block1=new Block(bytes,false);
SignatureValue newSig=new SignatureValue(block1);
System.out.println("new value: "+Arrays.toString(newSig.getValue()));
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
newSig.wireEncode(encoder);
System.out.println("new all: "+Arrays.toString(encoder.getBuffer()));
}
}
@@ -0,0 +1,31 @@
package minsecurity.crypto;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.junit.Test;
import static org.junit.Assert.*;
/*
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 14:56 2021/3/29
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class TestSM3 {
@Test
public void testSm3() {
HashAlgo hashAlgo = new HashAlgo();
String srcString = "123456";
byte[] srcData = srcString.getBytes();
byte[] resultHash = hashAlgo.sm3(srcData);
String sm3HexString = ByteUtils.toHexString(resultHash);
byte[] sm3Hash = ByteUtils.fromHexString(sm3HexString);
byte[] newHash = hashAlgo.sm3(srcString);
assertArrayEquals(sm3Hash,newHash);
}
}