publicKeyDecodeAll function

List<PublicKey> publicKeyDecodeAll(
  1. String str, {
  2. int offset = 0,
})

Decodes multiple public keys from text.

This function can be used to decode the OpenSSH authorized_keys file, which contains multiple public keys.

Returns all the public keys in str, starting at offset (or the beginning of the string if no offset is provided). If no public keys are found, an empty list is returned. As with publicKeyDecode the list contains instances of Pointy Castle PublicKey, whose type can be examined and then up-casted.

This method just keeps invoking the publicKeyDecode method until no more keys are found in the string. Each invocation starts parsing immediately after where the previous public key was found.

Implementation

List<pointy_castle.PublicKey> publicKeyDecodeAll(String str, {int offset = 0}) {
  final results = <pointy_castle.PublicKey>[];

  if (offset < 0) {
    throw ArgumentError.value(offset, 'offset', 'is negative');
  }
  var pos = offset;

// Keep trying to decode a text encoding until no more found

  while (pos < str.length) {
    try {
      final item = publicKeyDecode(str, offset: pos);
      results.add(item);
      if (item is RSAPublicKeyWithInfo) {
        pos = item.source!.end + 1;
      } else {
        throw StateError('publicKeyDecode returned unexpected type');
      }
    } catch (e) {
      rethrow;
    }
  }

  return results;
}