crypto.createPublicKey(key)
key
<Object> | <string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>key
: <string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <Object> 密钥材料,采用 PEM、DER 或 JWK 格式。format
: <string> 必须是'pem'
、'der'
或'jwk'
。 默认值:'pem'
。type
: <string> 必须是'pkcs1'
或'spki'
。 仅当format
为'der'
时才需要此选项,否则将被忽略。encoding
<string> 当key
是字符串时使用的字符串编码。
- 返回: <KeyObject>
创建并返回包含公钥的新密钥对象。
如果 key
是字符串或 Buffer
,则假定 format
是 'pem'
; 如果 key
是类型为 'private'
的 KeyObject
,则公钥来自给定的私钥; 否则,key
必须是具有上述属性的对象。
如果格式为 'pem'
,则 'key'
也可能是 X.509 证书。
因为公钥可以从私钥导出,所以可以传递私钥而不是公钥。
在这种情况下,此函数的行为就像 crypto.createPrivateKey()
已被调用,除了返回的 KeyObject
的类型将为 'public'
并且无法从返回的 KeyObject
中提取私钥。
同样,如果给定了类型为 'private'
的 KeyObject
,则新的类型为 'public'
的 KeyObject
将被返回,并且无法从返回的对象中提取私钥。
key
<Object> | <string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView>key
: <string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <Object> The key material, either in PEM, DER, or JWK format.format
: <string> Must be'pem'
,'der'
, or'jwk'
. Default:'pem'
.type
: <string> Must be'pkcs1'
or'spki'
. This option is required only if theformat
is'der'
and ignored otherwise.encoding
<string> The string encoding to use whenkey
is a string.
- Returns: <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.