Object built-in object

Object PKey

Asymmetric encryption algorithm object

The PKey object belongs to crypto Module, create:

1
var k = new crypto.PKey();

Inheritance

#lineWidth: 1.5 #font: Helvetica,sans-Serif #fontSize: 10 #leading: 1.6 #.this: fill=lightgray #.class: fill=white [<class>object|toString();toJSON()] [<this>PKey|new PKey()|name;keySize;publicKey|genRsaKey();genEcKey();genSm2Key();isPrivate();clone();importKey();importFile();exportPem();exportDer();exportJson();encrypt();decrypt();sign();verify()] [object] <:- [PKey] PKey new PKey() name keySize publicKey genRsaKey() genEcKey() genSm2Key() isPrivate() clone() importKey() importFile() exportPem() exportDer() exportJson() encrypt() decrypt() sign() verify() object toString() toJSON()

Constructor

PKey

PKey constructor

1
new PKey();

Construct PKey from DER format key

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

Call parameters:

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

Construct a PKey with a key in PEM format

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

Call parameters:

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

Construct a PKey with a key in JSON format

1
new PKey(Object jsonKey);

Call parameters:

  • jsonKey: Object, key in JSON format

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 current algorithm name

1
readonly String PKey.name;

keySize

Integer, returns the length of the current algorithm password, in bits

1
readonly Integer PKey.keySize;

publicKey

PKey, returns the public key of the current key

1
readonly PKey PKey.publicKey;

Return 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, specify the RSA key length, bit as the unit

genEcKey

Generate an EC private key

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

Call parameters:

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

genSm2Key

Generate an SM2 private key

1
PKey.genSm2Key() async;

isPrivate

Query whether the current key is a private key

1
Boolean PKey.isPrivate();

Return result:

  • Boolean, Is True, represents a private key

clone

Copy the current key

1
PKey PKey.clone();

Return result:

  • PKey, The copy object 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, decrypt 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, decrypt password

Load a key in JSON format

1
PKey.importKey(Object jsonKey);

Call parameters:

  • jsonKey: Object, key in JSON format

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, key file name
  • password: String, decrypt password

exportPem

Return the PEM format encoding of the current key

1
String PKey.exportPem();

Return result:

  • String, The PEM format encoding of the current key

exportDer

Returns the DER format encoding of the current key

1
Buffer PKey.exportDer();

Return result:

  • Buffer, The DER format encoding of the current key

exportJson

Returns the DER format encoding of the current key

1
Object PKey.exportJson();

Return 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, Specify the data to be encrypted

Return result:

  • Buffer, Return the encrypted data

decrypt

Use the current algorithm password private key to decrypt the data

1
Buffer PKey.decrypt(Buffer data) async;

Call parameters:

  • data: Buffer, Specify the data to be decrypted

Return result:

  • Buffer, Return the decrypted data

sign

Use the current algorithm password private key to sign data

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

Call parameters:

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

Return result:

  • Buffer, Return the signed data

verify

Use the current algorithm password public key to verify data

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

Call parameters:

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

Return result:

  • Boolean, Return the verified result

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be re-implemented according to its own characteristics

1
String PKey.toString();

Return result:

  • String, Returns the string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

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

Call parameters:

  • key: String, unused

Return result:

  • Value, Returns a value containing JSON serializable