encode method

  1. @override
Uint8List encode({
  1. ASN1EncodingRule encodingRule = ASN1EncodingRule.ENCODING_DER,
})
override

Encodes this ASN1Object depending on the given encodingRule

If no ASN1EncodingRule is given, ENCODING_DER will be used.

Supported encoding rules are :

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 octets = ascii.encode(stringValue!);
      valueByteLength = octets.length;
      valueBytes = Uint8List.fromList(octets);
      break;
    case ASN1EncodingRule.ENCODING_BER_CONSTRUCTED_INDEFINITE_LENGTH:
    case ASN1EncodingRule.ENCODING_BER_CONSTRUCTED:
      valueByteLength = 0;
      if (elements == null) {
        elements!.add(ASN1TeletextString(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);
}