创建并返回包含公钥的新密钥对象。 如果 key 是字符串或 Buffer,则假定 format'pem'; 如果 key 是类型为 'private'KeyObject,则公钥来自给定的私钥; 否则,key 必须是具有上述属性的对象。

如果格式为 'pem',则 'key' 也可能是 X.509 证书。

因为公钥可以从私钥导出,所以可以传递私钥而不是公钥。 在这种情况下,此函数的行为就像 crypto.createPrivateKey() 已被调用,除了返回的 KeyObject 的类型将为 'public' 并且无法从返回的 KeyObject 中提取私钥。 同样,如果给定了类型为 'private'KeyObject,则新的类型为 'public'KeyObject 将被返回,并且无法从返回的对象中提取私钥。

Creates and returns a new key object containing a public key. If key is a string or Buffer, format is assumed to be 'pem'; if key is a KeyObject with type 'private', the public key is derived from the given private key; otherwise, key must be an object with the properties described above.

If the format is 'pem', the 'key' may also be an X.509 certificate.

Because public keys can be derived from private keys, a private key may be passed instead of a public key. In that case, this function behaves as if crypto.createPrivateKey() had been called, except that the type of the returned KeyObject will be 'public' and that the private key cannot be extracted from the returned KeyObject. Similarly, if a KeyObject with type 'private' is given, a new KeyObject with type 'public' will be returned and it will be impossible to extract the private key from the returned object.