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:
var l = <int>[];
for (var c in stringValue!.codeUnits) {
l.add(0);
l.add(c);
}
valueBytes = Uint8List.fromList(l);
break;
case ASN1EncodingRule.ENCODING_BER_CONSTRUCTED_INDEFINITE_LENGTH:
case ASN1EncodingRule.ENCODING_BER_CONSTRUCTED:
valueByteLength = 0;
if (elements == null) {
elements!.add(ASN1BMPString(stringValue: stringValue));
}
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);
}