encode method

  1. @override
String encode({
  1. bool doNotQuoteComments = false,
})
override

Encode into text

Implementation

@override
String encode({bool doNotQuoteComments = false}) {
  final buf = StringBuffer('$beginMarker\n');

  // Headers

  for (final h in headers) {
    var value = h.value;

    // Remove line breaks, the encoding does not support values with them
    value = value.replaceAll('\r', ' ').replaceAll('\n', ' ');

    if (!doNotQuoteComments) {
      // RFC 4716 says:
      // Currently, common practice is to quote the Header-value of the
      //   Comment by prefixing and suffixing it with '"' characters, and some
      //   existing implementations fail if these quotation marks are omitted.
      //
      //   Compliant implementations MUST function correctly if the quotation
      //   marks are omitted.
      //
      //   Implementations MAY include the quotation marks.  If the first and
      //   last characters of the Header-value are matching quotation marks,
      //   implementations SHOULD remove them before using the value.

      if (h.tag.toLowerCase() == SshPublicKeyHeader.commentTag) {
        value = '"$value"';
      }
    }

    final str = '${h.tag}: $value';

    var p = 0;
    while (p < str.length) {
      int endPos;
      if ((p + 72 < str.length)) {
        // continuation line
        endPos = p + 71;
        buf
          ..write(str.substring(p, endPos))
          ..write('\\\n');
      } else {
        // final line
        endPos = str.length;
        buf
          ..write(str.substring(p, endPos))
          ..write('\n');
      }
      p = endPos;
    }
  }
  // Encapsulated text

  final b64 = base64.encode(bytes);
  var p = 0;
  while (p < b64.length) {
    final endPos = (p + 72 < b64.length) ? (p + 72) : b64.length;
    buf
      ..write(b64.substring(p, endPos))
      ..write('\n');
    p = endPos;
  }

  // End marker

  buf.write('$_endMarker\n');

  return buf.toString();
}