crypto.createPublicKey(key)


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

【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.】

如果格式为‘pem’,‘key’也可以是 X.509 证书。

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

由于公钥可以从私钥派生,因此可以传递私钥而不是公钥。在这种情况下,该函数的行为就像调用了 crypto.createPrivateKey(),只是返回的 KeyObject 的类型将是 'public',且无法从返回的 KeyObject 中提取私钥。类似地,如果提供了类型为 'private'KeyObject,将返回一个类型为 'public' 的新 KeyObject,并且无法从返回的对象中提取私钥。

【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.】