Files
min-dev-java/src/main/java/component/SignatureValue.java
T

124 lines
3.6 KiB
Java

package component;
import encoding.*;
/*
* @Author: Wang Feng
* @Description: 表示一个签名值 TLV
// 1. 格式如下:
// SignatureValue = 200 TLV-LENGTH *OCTET
// 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
* @Version: 1.0.0
* @Date: 21:45 2021/3/12
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SignatureValue implements TlvComponentBase, IEncodingAble {
private byte[] value;
/**
* 根据签名值字节数组创建一个 SignatureValue
* @param value
*/
public SignatureValue(byte[] value) throws ComponentException {
if(!this.buildSignatureValueByByteArray(value)){
throw new ComponentException("Signature(byte[] value): value invalid");
}
}
/**
* 根据 TLV Block 创建一个 SignatureValue
* @param block
*/
public SignatureValue(Block block) throws ComponentException {
if(!this.buildSignatureValueByBlock(block)){
throw new ComponentException("Signature(Block block): block invalid");
}
}
/**
* 根据签名值字节数组初始化一个 SignatureValue
* @param value
* @return
*/
public boolean buildSignatureValueByByteArray(byte[] value){
this.value=new byte[value.length];
System.arraycopy(value,0,this.value,0,value.length);
return true;
}
/**
* 根据 TLV Block 初始化一个 SignatureValue
* @param block
* @return
*/
public boolean buildSignatureValueByBlock(Block block) throws ComponentException {
return this.wireDecode(block);
}
/**
* 得到签名值的字节数组
* @return
*/
public byte[] getValue(){
return this.value;
}
/**
* 对 SignatureValue 进行线速编码为TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try {
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());
}
}
/**
* 从 TLV Block 中解码出一个 SignatureValue
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
try {
// 检查 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());
}
}
}