encode method
Encodes this ASN1Object depending on the given encodingRule
If no ASN1EncodingRule is given, ENCODING_DER will be used.
Supported encoding rules are :
- ASN1EncodingRule.ENCODING_DER
- ASN1EncodingRule.ENCODING_BER_LONG_LENGTH_FORM
- ASN1EncodingRule.ENCODING_BER_CONSTRUCTED
- ASN1EncodingRule.ENCODING_BER_CONSTRUCTED_INDEFINITE_LENGTH
Throws an UnsupportedAsn1EncodingRuleException if the given encodingRule
is not supported.
Implementation
@override
Uint8List encode(
{ASN1EncodingRule encodingRule = ASN1EncodingRule.ENCODING_DER}) {
switch (encodingRule) {
case ASN1EncodingRule.ENCODING_DER:
case ASN1EncodingRule.ENCODING_BER_LONG_LENGTH_FORM:
valueByteLength = octets!.length;
valueBytes = octets;
break;
case ASN1EncodingRule.ENCODING_BER_CONSTRUCTED:
case ASN1EncodingRule.ENCODING_BER_CONSTRUCTED_INDEFINITE_LENGTH:
valueByteLength = 0;
if (elements == null) {
elements!.add(ASN1OctetString(octets: octets));
}
valueByteLength = _childLength(
isIndefinite: encodingRule ==
ASN1EncodingRule.ENCODING_BER_CONSTRUCTED_INDEFINITE_LENGTH);
valueBytes = Uint8List(valueByteLength!);
var i = 0;
for (var obj in elements!) {
var b = obj.encode();
valueBytes!.setRange(i, i + b.length, b);
i += b.length;
}
break;
case ASN1EncodingRule.ENCODING_BER_PADDED:
throw UnsupportedAsn1EncodingRuleException(encodingRule);
}
return super.encode(encodingRule: encodingRule);
}