finished component: KeyLocator

This commit is contained in:
free will committed 2021-03-12 19:23:44 +08:00
1 parent 7a687a4564
commit 417c08db7f
1 file changed
+156
+156
View File
@@ -0,0 +1,156 @@
package component;
import encoding.*;
/*
* @Author: Wang Feng
* @Description: 表示一个用于签名的证书或公钥的位置
// 1. 格式如下:
// KeyLocator = KEYLOCATOR-TYPE TLV-TYPE <Identifier>
// 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
* @Version: 1.0.0
* @Date: 13:20 2021/3/12
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class KeyLocator implements TlvComponentBase, IEncodingAble {
private Identifier identifier;
/**
* 根据一个 TLV Block 创建一个 KeyLocator
* @param block
*/
public KeyLocator(Block block){
this.buildKeyLocatorByBlock(block);
}
/**
* 根据标识字符串 创建一个 KeyLocator
* @param identifierString
*/
public KeyLocator(String identifierString){
this.buildKeyLocatorByString(identifierString);
}
/**
* 根据 components 创建一个 KeyLocator
* @param container
*/
public KeyLocator(IdentifierComponentContainer container){
this.buildKeyLocatorByComponents(container);
}
/**
* 根据 Identifier 创建一个 KeyLocator
* @param identifier
*/
public KeyLocator(Identifier identifier){
this.buildKeyLocatorByIdentifier(identifier);
}
/**
* 根据一个 TVL Block 初始化当前 KeyLocator
* @param block
* @return
*/
public boolean buildKeyLocatorByBlock(Block block){
return this.wireDecode(block);
}
/**
* 根据一个标识字符串初始化当前 KeyLocator
* @param identifierString
* @return
*/
public boolean buildKeyLocatorByString(String identifierString){
return this.identifier.buildIdentifierByString(identifierString);
}
/**
* 根据 components 初始化当前 KeyLocator
* @param container
* @return
*/
public boolean buildKeyLocatorByComponents(IdentifierComponentContainer container){
return this.identifier.buildIdentifierByComponents(container);
}
/**
* 根据 Identifier 初始化当前 KeyLocator
* @param identifier
* @return
*/
public boolean buildKeyLocatorByIdentifier(Identifier identifier){
return this.identifier.buildIdentifierByComponents(identifier.getComponents());
}
/**
* 获取用于签名的证书或公钥的标识名(指示了位置)
* @return
*/
public Identifier getIdentifier(){
return this.identifier;
}
/**
* 判断当前 KeyLocator 是否有效
* @return
*/
public boolean isValid(){
return this.identifier.isValid();
}
/**
* 将 KeyLocator 线速编码为 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=this.identifier.wireEncode(encoder);
if(tmplen<=0){
return 0;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen<=0){
return 0;
}
totalLength+=tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 KeyLocator
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) {
// 检查Type是否正确
if(!TLV.isValidIdentifierType(block.getType())){
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));
}
}