publicKeyDecode function

PublicKey publicKeyDecode(
  1. String str, {
  2. int offset = 0,
  3. bool allowPreamble = false,
})

Decodes the first public key from text.

Result

Returns first public key in str, starting at offset (or the beginning of the string if no offset is provided).

If there are multiple public keys in the string, only the first is returned. The others can be obtained by invoking this function again, with an offset into the string that is after that first public key: which can be obtained by examining the source member in the result.

Returns a Pointy Castle PublicKey, which is an abstract class. The program should determine what the actual type is and then cast it into that type. For example,

final k = publicKeyDecode(str);
if (k is RSAPublicKeyWithInfo) {
  // use the RSA public key object

} else if (k is GenericPublicKey) {
  // use the generic public key object
  // this type is returned for the OpenSSH Public Key format (i.e. the
  // one line format) when the key-type is not recognised
  // (i.e. when the key-type is not "ssh-rsa").
}

The text before and after the public key can be identified by examining the source member in the result (after casting it into its actual type).

Whitespace

Any whitespace before the public key is always ignored.

Preamble before the public key can be ignored if allowPreamble is set to true. It is set to false by default, which means the public key must start with the first non-whitespace character. Only some public key formats allow preamble, which is arbitrary text that before the public key (these formats are those where the start of the public key can be detected, such as those that start with a "-----BEGIN ...-----" line).

Exceptions

A KeyMissing is thrown if no public key is found.

A KeyBad is thrown if the private key is invalid.

A KeyUnsupported is thrown if the type of public key is not supported.

See also

This method decodes one public key. If multiple public keys are expected in the string use the publicKeyDecodeAll method.

Implementation

pointy_castle.PublicKey publicKeyDecode(String str,
    {int offset = 0, bool allowPreamble = false}) {
  if (offset < 0) {
    throw ArgumentError.value(offset, 'offset', 'is negative');
  }

  KeyException? exceptionFromTryingOpenSshFormat;

  final p = _skipWhitespace(str, offset); // skip leading whitespace
  if (p != str.length) {
    // Phase 1: try OpenSSH format.
    //
    // Since it is the only format that cannot skip preamble, if it exists in
    // the string it must start at the very first non-whitespace character.

    try {
      final found = _publicKeyDecodeOpenSshFormat(str, p);
      if (found != null) {
        return found;
      }
    } on KeyException catch (e) {
      // Save exception for use after trying the non-OpenSSH format
      // This exception contains more detail about what went wrong, if it was
      // trying to parse an OpenSSH format public key.
      exceptionFromTryingOpenSshFormat = e;
    }

    // Phase 2: try the other (i.e. non-OpenSSH public key) formats.
    //
    // This must be tried AFTER trying the OpenSSH public key format, in case the
    // string contained multiple public keys and there was an OpenSSH public key
    // format before the public key in the other format. If [allowPreamble] is
    // true, it could treat that OpenSSH public key as preamble and skip over it.
    // Having multiple public keys, with different formats, in the same string
    // is highly unlikely, but this code can correctly handle it.

    final found =
        _publicKeyDecodeOtherFormats(str, p, allowPreamble: allowPreamble);
    if (found != null) {
      return found;
    }
  }

  throw exceptionFromTryingOpenSshFormat ?? KeyMissing('no public key found');
}