Object built-in object

Object PKey

Asymmetric encryption algorithm object

The PKey object belongs to the crypto module and is created:

1
var k = new crypto.PKey();

Inheritance

Constructor

PKey

PKey constructor

1
new PKey();

Constructing a PKey from a DER-formatted key

1 2
new PKey(Buffer DerKey, String password = "");

Call parameters:

  • DerKey : Buffer , DER format key
  • password : String, decryption password

Constructing a PKey from a key in PEM format

1 2
new PKey(String pemKey, String password = "");

Call parameters:

  • pemKey : String, key in PEM format
  • password : String, decryption password

Constructing a PKey from a JSON-formatted key

1
new PKey(Object jsonKey);

Call parameters:

  • jsonKey : Object, JSON format key

The format of jsonKey supports the following four, RSA private keys:

1 2 3 4 5 6 7 8 9 10 11
{ "kty": "RSA", "n": "0m5lvKpWqy9JS7tV2HIPqHCYHLquSuxIC3F8strIQLJKO3rZmTT96KTnhsOfBO7Y1bI7mnT0PB3_vcHd9ekWMEoZJQw7MuB8KeM_Wn54-elJr5DNLk5bMppSGxX7ZnumiXGG51_X3Yp-_EbGtDG80GxXXix7Mucyo7K25uE0uW8=", "e": "AQAB", "d": "agN2O9NxMHL1MTMi75WfL9Pxvl-KWXKqZSF6mjzAsF9iKI8euyHIXYFepzU8kual1RsjDhCnzvWqFvZplW8lXqrHf_P-rS_9Y4gBUw6pjnI_DnFIRwWHRvrUHHSCfWOdTCIKdOTkgLZuGFuhEY3RMIW0WSYejjLtftwy0RVxAzk=", "p": "6a4G1qmfwWmn1biigN7IVFlkbLf9oVe6g7rOmHxI-hn1GRxKDSVuAUrmR1IhuAnca9M0y7SD-7TUs6wjOxWxaw==", "q": "5ofkxFKdPBD0CQHMb9q13AMHUVe0rJ-hSjqqIBrmqApUOneyAcMV76M0QyIQnI2p3POa4Qu_7XChDwRVl7LlDQ==", "dp": "2mXGiGwCHl8j-FBWuID-1C6z-BRB3MBEVoeKPOOzxOPruatB3mWEGXsqG7A8SWgV9URxTI2K6P3J6Z7RUpBkvw==", "dq": "oagn5vfb5NQqnOpS9xkSsD67cfIj821ZSFlNFYhnuOzNVda7z_qCtnHm4zDPH0lEFXoKYMfBhfqWJpaugttjPQ==", "qi": "dqEQgxNmOVFrF4s776hTqeC6oEDila8EvpVb2F2ZvwAOLjCQ66OiAZK1BiYGHqUy0NeqNmtlsLSuBEZQZvqZwg==" }

RSA public key:

1 2 3 4 5
{ "kty": "RSA", "n": "0m5lvKpWqy9JS7tV2HIPqHCYHLquSuxIC3F8strIQLJKO3rZmTT96KTnhsOfBO7Y1bI7mnT0PB3_vcHd9ekWMEoZJQw7MuB8KeM_Wn54-elJr5DNLk5bMppSGxX7ZnumiXGG51_X3Yp-_EbGtDG80GxXXix7Mucyo7K25uE0uW8=", "e": "AQAB" }

EC private key:

1 2 3 4 5 6 7
{ "kty": "EC", "crv": "P-521", "x": "ATfNNFuuvlGxrTGoXgyfSAGgRNNDnO3rN3k74urKJdVS14RYhdnSwm91Bm-F1l-T1XKlAY2yRnzG9w1Ukvo8c0wL", "y": "ASBHqrruB6kdkEUB3vlW3-UIkk4HtKdUeTwN-7m3j2rgZvYR1ffRAapDvWqKGiBjomqWafxokBkbDI0c95f6f4XU", "d": "AfkIbUHXfW41njdpoKuqqKludcoLJS8D_oMEwkj-GVaXFNKccIoF5iKGu2c69kNDjo83R_7wyGlfRczsklkik1ST" }

EC public key:

1 2 3 4 5 6
{ "kty": "EC", "crv": "P-521", "x": "ATfNNFuuvlGxrTGoXgyfSAGgRNNDnO3rN3k74urKJdVS14RYhdnSwm91Bm-F1l-T1XKlAY2yRnzG9w1Ukvo8c0wL", "y": "ASBHqrruB6kdkEUB3vlW3-UIkk4HtKdUeTwN-7m3j2rgZvYR1ffRAapDvWqKGiBjomqWafxokBkbDI0c95f6f4XU" }

Member attributes

name

String, returns the name of the current algorithm

1
readonly String PKey.name;

keySize

Integer, returns the current algorithm password length, in bits

1
readonly Integer PKey.keySize;

publicKey

PKey, returns the public key of the current key

1
readonly PKey PKey.publicKey;

Returns the result:

  • The public key of the current key

Member function

genRsaKey

Generate an RSA private key

1
PKey.genRsaKey(Integer size) async;

Call parameters:

  • size : Integer, specifies the RSA key length, in bits

genEcKey

Generate an EC private key

1
PKey.genEcKey(String curve = "secp521r1") async;

Call parameters:

  • curve : String, specify the preset elliptic curve, the optional values ​​are: "secp521r1", "brainpoolP512r1", "secp384r1", "brainpoolP384r1", "secp256r1", "secp256k1", "brainpoolP256r1", "secp224r1", "secp224k1" , "secp192r1", "secp192k1"

isPrivate

Query whether the current key is a private key

1
Boolean PKey.isPrivate();

Returns the result:

  • Boolean for True as a private key

clone

Copy the current key

1
PKey PKey.clone();

Returns the result:

  • PKey , a copy of the current key

importKey

Load a key in DER format

1 2
PKey.importKey(Buffer DerKey, String password = "");

Call parameters:

  • DerKey : Buffer , DER format key
  • password : String, decryption password

Load a key in PEM format

1 2
PKey.importKey(String pemKey, String password = "");

Call parameters:

  • pemKey : String, key in PEM format
  • password : String, decryption password

Load a key in JSON format

1
PKey.importKey(Object jsonKey);

Call parameters:

  • jsonKey : Object, JSON format key

The format of jsonKey supports the following four, RSA private keys:

1 2 3 4 5 6 7 8 9 10 11
{ "kty": "RSA", "n": "0m5lvKpWqy9JS7tV2HIPqHCYHLquSuxIC3F8strIQLJKO3rZmTT96KTnhsOfBO7Y1bI7mnT0PB3_vcHd9ekWMEoZJQw7MuB8KeM_Wn54-elJr5DNLk5bMppSGxX7ZnumiXGG51_X3Yp-_EbGtDG80GxXXix7Mucyo7K25uE0uW8=", "e": "AQAB", "d": "agN2O9NxMHL1MTMi75WfL9Pxvl-KWXKqZSF6mjzAsF9iKI8euyHIXYFepzU8kual1RsjDhCnzvWqFvZplW8lXqrHf_P-rS_9Y4gBUw6pjnI_DnFIRwWHRvrUHHSCfWOdTCIKdOTkgLZuGFuhEY3RMIW0WSYejjLtftwy0RVxAzk=", "p": "6a4G1qmfwWmn1biigN7IVFlkbLf9oVe6g7rOmHxI-hn1GRxKDSVuAUrmR1IhuAnca9M0y7SD-7TUs6wjOxWxaw==", "q": "5ofkxFKdPBD0CQHMb9q13AMHUVe0rJ-hSjqqIBrmqApUOneyAcMV76M0QyIQnI2p3POa4Qu_7XChDwRVl7LlDQ==", "dp": "2mXGiGwCHl8j-FBWuID-1C6z-BRB3MBEVoeKPOOzxOPruatB3mWEGXsqG7A8SWgV9URxTI2K6P3J6Z7RUpBkvw==", "dq": "oagn5vfb5NQqnOpS9xkSsD67cfIj821ZSFlNFYhnuOzNVda7z_qCtnHm4zDPH0lEFXoKYMfBhfqWJpaugttjPQ==", "qi": "dqEQgxNmOVFrF4s776hTqeC6oEDila8EvpVb2F2ZvwAOLjCQ66OiAZK1BiYGHqUy0NeqNmtlsLSuBEZQZvqZwg==" }

RSA public key:

1 2 3 4 5
{ "kty": "RSA", "n": "0m5lvKpWqy9JS7tV2HIPqHCYHLquSuxIC3F8strIQLJKO3rZmTT96KTnhsOfBO7Y1bI7mnT0PB3_vcHd9ekWMEoZJQw7MuB8KeM_Wn54-elJr5DNLk5bMppSGxX7ZnumiXGG51_X3Yp-_EbGtDG80GxXXix7Mucyo7K25uE0uW8=", "e": "AQAB" }

EC private key:

1 2 3 4 5 6 7
{ "kty": "EC", "crv": "P-521", "x": "ATfNNFuuvlGxrTGoXgyfSAGgRNNDnO3rN3k74urKJdVS14RYhdnSwm91Bm-F1l-T1XKlAY2yRnzG9w1Ukvo8c0wL", "y": "ASBHqrruB6kdkEUB3vlW3-UIkk4HtKdUeTwN-7m3j2rgZvYR1ffRAapDvWqKGiBjomqWafxokBkbDI0c95f6f4XU", "d": "AfkIbUHXfW41njdpoKuqqKludcoLJS8D_oMEwkj-GVaXFNKccIoF5iKGu2c69kNDjo83R_7wyGlfRczsklkik1ST" }

EC public key:

1 2 3 4 5 6
{ "kty": "EC", "crv": "P-521", "x": "ATfNNFuuvlGxrTGoXgyfSAGgRNNDnO3rN3k74urKJdVS14RYhdnSwm91Bm-F1l-T1XKlAY2yRnzG9w1Ukvo8c0wL", "y": "ASBHqrruB6kdkEUB3vlW3-UIkk4HtKdUeTwN-7m3j2rgZvYR1ffRAapDvWqKGiBjomqWafxokBkbDI0c95f6f4XU" }

importFile

Load a key file in PEM / DER format

1 2
PKey.importFile(String filename, String password = "");

Call parameters:

  • filename : String, the key file name
  • password : String, decryption password

exportPem

Returns the PEM format encoding of the current key

1
String PKey.exportPem();

Returns the result:

  • String , the PEM format encoding of the current key

exportDer

Returns the DER format encoding of the current key

1
Buffer PKey.exportDer();

Returns the result:

  • Buffer , the DER format encoding of the current key

exportJson

Returns the DER format encoding of the current key

1
Object PKey.exportJson();

Returns the result:

  • Object , the DER format encoding of the current key

encrypt

Encrypt data using the current algorithm password public key

1
Buffer PKey.encrypt(Buffer data) async;

Call parameters:

  • data : Buffer , specifies the data to be encrypted

Returns the result:

  • Buffer , return the encrypted data

decrypt

Decrypt data using current algorithm password private key

1
Buffer PKey.decrypt(Buffer data) async;

Call parameters:

  • data : Buffer , specifies the data to be decrypted

Returns the result:

  • Buffer , return the decrypted data

sign

Sign data with the current algorithm password private key

1 2
Buffer PKey.sign(Buffer data, Integer alg = 0) async;

Call parameters:

  • data : Buffer , specifies the data to be signed
  • alg : Integer, specify the algorithm to be signed, default 0. Supported algorithms: 0 = NONE, 1 = MD2,2 = MD4,3 = MD5,4 = SHA1,5 = SHA224,6 = SHA256,7 = SHA384,8 = SHA512,9 = RIPEMD160

Returns the result:

  • Buffer , return the signed data

verify

Validate data using current algorithm password public key

1 2 3
Boolean PKey.verify(Buffer data, Buffer sign, Integer alg = 0) async;

Call parameters:

  • data : Buffer , specifies the data to be verified
  • sign : Buffer , specifies the signature to verify
  • alg : Integer, specify the algorithm to be signed, default 0. Supported algorithms: 0 = NONE, 1 = MD2,2 = MD4,3 = MD5,4 = SHA1,5 = SHA224,6 = SHA256,7 = SHA384,8 = SHA512,9 = RIPEMD160

Returns the result:

  • Boolean , return the result after verification

toString

Returns a string representation of the object, usually "[Native Object]". Objects can be reimplemented according to their own characteristics

1
String PKey.toString();

Returns the result:

  • String , returns a string representation of the object

toJSON

Returns a JSON representation of the object, typically returning a collection of readable properties defined by the object

1
Value PKey.toJSON(String key = "");

Call parameters:

  • key : String, unused

Returns the result:

  • Value , returns a value containing a JSON serializable