finish Component: SignatureType

This commit is contained in:
free will committed 2021-03-15 20:25:10 +08:00
1 parent 92408238aa
commit 236aecf91d
1 file changed
+120 -10
+120 -10
View File
@@ -1,8 +1,8 @@
package component;
import encoding.Block;
import encoding.Encoder;
import encoding.IEncodingAble;
import encoding.*;
import minsecurity.Common;
import org.omg.CORBA.PRIVATE_MEMBER;
/*
* @Author: Wang Feng
@@ -16,29 +16,139 @@ import encoding.IEncodingAble;
*/
class SignatureTypeValue{
public long getSignatureTypeValue() {
private int signatureTypeValue;
public SignatureTypeValue(int signatureTypeValue){
this.signatureTypeValue=signatureTypeValue;
}
public int getSignatureTypeValue() {
return signatureTypeValue;
}
public void setSignatureTypeValue(long signatureTypeValue) {
public void setSignatureTypeValue(int signatureTypeValue) {
this.signatureTypeValue = signatureTypeValue;
}
}
class SignatureAlgorithm{
public int getSignatureAlgorithm() {
return signatureAlgorithm;
}
private long signatureTypeValue;
public SignatureTypeValue(long signatureTypeValue){
this.signatureTypeValue=signatureTypeValue;
public void setSignatureAlgorithm(int signatureAlgorithm) {
this.signatureAlgorithm = signatureAlgorithm;
}
private int signatureAlgorithm;
public SignatureAlgorithm(int value){
this.signatureAlgorithm=value;
}
}
public class SignatureType implements TlvComponentBase, IEncodingAble {
// private SignatureAlgorithm value;
private SignatureAlgorithm value; // SignatureAlgorithm
/**
* 根据签名类型值创建一个 SignatureType
* @param signatureTypeValue
*/
public SignatureType(SignatureAlgorithm signatureTypeValue){
this.buildSignatureTypeByValue(signatureTypeValue);
}
/**
* 根据 TLV Block 创建一个 SignatureType
* @param block
*/
public SignatureType(Block block){
this.buildSignatureTypeByBlock(block);
}
/**
* 根据签名类型值初始化一个 SignatureType
* // @Description:
* // 1. 值应该是已定义的
* @param signatureTypeValue
* @return
*/
public boolean buildSignatureTypeByValue(SignatureAlgorithm signatureTypeValue){
// 检查签名类型值是否已知 todo: min-lib-go中判断的是minsecurity.SM2WithSM3
if(signatureTypeValue.getSignatureAlgorithm()!= Common.SM3withSM2){
throw new Error("SignatureType.buildSignatureTypeByValue: Unknown signature type "
+signatureTypeValue.getSignatureAlgorithm());
}
this.value=signatureTypeValue;
return true;
}
/**
* 根据 TLV Block 初始化一个 SignatureType
* @param block
* @return
*/
public boolean buildSignatureTypeByBlock(Block block){
return this.wireDecode(block);
}
/**
* 获取签名类型值
* @return
*/
public SignatureAlgorithm getValue(){
return this.value;
}
/**
* 将 SignatureType 线速编码为 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
return 0;
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
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(TLV.TlvSignatureType));
if(tmplen<=0){
return 0;
}
totalLength+=tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 SignatureType
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) {
return false;
// 检查 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;
}
}