Module Basic module

Module crypto

Encryption algorithm module

Instructions:

1
var crypto = require('crypto');

Object

Cipher

Cipher constructor, see Cipher

1
Cipher crypto.Cipher;

PKey

PKey constructor, see PKey

1
PKey crypto.PKey;

X509Cert

X509Cert constructor, see X509Cert

1
X509Cert crypto.X509Cert;

X509Crl

X509Crl constructor, see X509Crl

1
X509Crl crypto.X509Crl;

X509Req

X509Req constructor, see X509Req

1
X509Req crypto.X509Req;

Static function

createHash

Create a message summary object based on the given algorithm name

1
static Digest crypto.createHash(String algo);

Call parameters:

  • algo : String, specifies the algorithm of the message digest object

Return result:

  • Digest , return information summary object

createHmac

Create a hmac information summary object according to the given algorithm name

1 2
static Digest crypto.createHmac(String algo, Buffer key);

Call parameters:

  • algo : String, specifies the algorithm of the message digest object
  • key : Buffer , binary signature key

Return result:

  • Digest , return information summary object

loadPKey

Load a key file in PEM/DER format

1 2
static PKey crypto.loadPKey(String filename, String password = "");

Call parameters:

  • filename : String, key file name
  • password : String, decrypt password

Return result:

  • PKey , return the object containing the key

loadCert

Load a certificate in CRT/PEM/DER/TXT format, which can be called multiple times

1
static X509Cert crypto.loadCert(String filename);

Call parameters:

  • filename : String, certificate file name

Return result:

  • X509Cert , returns the object containing the certificate

loadFile loads mozilla's certdata, txt, available for download at http


loadCrl

Load a revocation certificate in PEM/DER format, which can be called multiple times

1
static X509Crl crypto.loadCrl(String filename);

Call parameters:

  • filename : String, revoked certificate file name

Return result:

  • X509Crl , return object containing revoked certificate

loadReq

Load a PEM/DER format certificate request, which can be called multiple times

1
static X509Req crypto.loadReq(String filename);

Call parameters:

  • filename : String, the certificate request file name

Return result:

  • X509Req , returns the object containing the requested certificate

randomBytes

Generate random numbers of specified size, use havege generator

1
static Buffer crypto.randomBytes(Integer size) async;

Call parameters:

  • size : Integer, specify the size generated random number

Return result:

  • Buffer , return the generated random number

simpleRandomBytes

Generate low-intensity random numbers of specified size, using a fast algorithm

1
static Buffer crypto.simpleRandomBytes(Integer size) async;

Call parameters:

  • size : Integer, specify the size generated random number

Return result:

  • Buffer , return the generated random number

pseudoRandomBytes

Generate pseudo-random numbers of specified size, using entropy generator

1
static Buffer crypto.pseudoRandomBytes(Integer size) async;

Call parameters:

  • size : Integer, specify the size generated random number

Return result:

  • Buffer , return the generated random number

randomArt

Generate visual character images for given data

1 2 3
static String crypto.randomArt(Buffer data, String title, Integer size = 8);

Call parameters:

  • data : Buffer , specify the data to be displayed
  • title : String, the title of the specified character image, multi-byte characters will cause the wrong width
  • size : Integer, character image size

Return result:

  • String , return the generated visual string image

pbkdf1

Generate the required binary key based on the plaintext password according to pbkdf1

1 2 3 4 5
static Buffer crypto.pbkdf1(Buffer password, Buffer salt, Integer iterations, Integer size, Integer algo) async;

Call parameters:

  • password : Buffer , specify the password to use
  • salt : Buffer , specify the salt used by hmac
  • iterations : Integer, specify the number of iterations
  • size : Integer, specify the key size
  • algo : Integer, specify the hash algorithm to use, see the hash module for details

Return result:

  • Buffer , returns the generated binary key

Generate the required binary key based on the plaintext password according to pbkdf1

1 2 3 4 5
static Buffer crypto.pbkdf1(Buffer password, Buffer salt, Integer iterations, Integer size, String algoName) async;

Call parameters:

  • password : Buffer , specify the password to use
  • salt : Buffer , specify the salt used by hmac
  • iterations : Integer, specify the number of iterations
  • size : Integer, specify the key size
  • algoName : String, specify the hash algorithm to use, see the hash module for details

Return result:

  • Buffer , returns the generated binary key

pbkdf2

Generate the required binary key based on the plaintext password according to rfc2898

1 2 3 4 5
static Buffer crypto.pbkdf2(Buffer password, Buffer salt, Integer iterations, Integer size, Integer algo) async;

Call parameters:

  • password : Buffer , specify the password to use
  • salt : Buffer , specify the salt used by hmac
  • iterations : Integer, specify the number of iterations
  • size : Integer, specify the key size
  • algo : Integer, specify the hash algorithm to use, see the hash module for details

Return result:

  • Buffer , returns the generated binary key

Generate the required binary key based on the plaintext password according to rfc2898

1 2 3 4 5
static Buffer crypto.pbkdf2(Buffer password, Buffer salt, Integer iterations, Integer size, String algoName) async;

Call parameters:

  • password : Buffer , specify the password to use
  • salt : Buffer , specify the salt used by hmac
  • iterations : Integer, specify the number of iterations
  • size : Integer, specify the key size
  • algoName : String, specify the hash algorithm to use, see the hash module for details

Return result:

  • Buffer , returns the generated binary key

constant

AES

Specify symmetric encryption algorithm AES, support 128, 192, 256-bit key, block cipher working mode support ECB, CBC, CFB128, CTR, GCM, CCM, XTS

1
const crypto.AES = 1;

CAMELLIA

Specify symmetric encryption algorithm CAMELLIA, support 128, 192, 256-bit key, block cipher working mode support ECB, CBC, CFB128, CTR, GCM, CCM

1
const crypto.CAMELLIA = 2;

DES

Specify symmetric encryption algorithm DES, support 64-bit key, block cipher working mode support ECB, CBC

1
const crypto.DES = 3;

DES_EDE

Specify symmetric encryption algorithm DES-EDE, support 128-bit key, block cipher working mode support ECB, CBC

1
const crypto.DES_EDE = 4;

DES_EDE3

Specify symmetric encryption algorithm DES-EDE3, support 192-bit key, block cipher working mode support ECB, CBC

1
const crypto.DES_EDE3 = 5;

BLOWFISH

Specify symmetric encryption algorithm BLOWFISH, support 192-bit key, block cipher working mode support ECB, CBC, CFB64, CTR

1
const crypto.BLOWFISH = 6;

ARC4

Specify symmetric encryption algorithm ARC4, support 40, 56, 64, 128 key

1
const crypto.ARC4 = 7;

ARIA

Specify symmetric encryption algorithm ARIA, support 128, 192, 256-bit key, block cipher working mode support ECB, CBC, CFB128, CTR, GCM, CCM

1
const crypto.ARIA = 8;

CHACHA20

Specify symmetric encryption algorithm CHACHA20, support 256-bit key, block cipher working mode support POLY1305

1
const crypto.CHACHA20 = 9;

SM4

Specify symmetric encryption algorithm SM4, block cipher working mode supports ECB, CBC

1
const crypto.SM4 = 10;

ECB

Specify block cipher working mode to support ECB

1
const crypto.ECB = 1;

CBC

Specify block cipher working mode to support CBC

1
const crypto.CBC = 2;

CFB64

Specify block cipher working mode to support CFB64

1
const crypto.CFB64 = 3;

CFB128

Specify block cipher working mode to support CFB128

1
const crypto.CFB128 = 4;

OFB

Specify the block cipher working mode to support OFB

1
const crypto.OFB = 5;

CTR

Specify block cipher working mode to support CTR

1
const crypto.CTR = 6;

GCM

Specify block cipher working mode to support GCM

1
const crypto.GCM = 7;

STREAM

Specify stream cipher mode

1
const crypto.STREAM = 8;

CCM

Specify block cipher working mode to support CCM

1
const crypto.CCM = 9;

XTS

Specified block cipher working mode supports XTS

1
const crypto.XTS = 10;

POLY1305

Specify block cipher working mode to support POLY1305

1
const crypto.POLY1305 = 11;

PKCS7

Specify the fill mode as PKCS7

1
const crypto.PKCS7 = 0;

ONE_AND_ZEROS

Specify the fill mode as ONE_AND_ZEROS

1
const crypto.ONE_AND_ZEROS = 1;

ZEROS_AND_LEN

Specify the fill mode as ZEROS_AND_LEN

1
const crypto.ZEROS_AND_LEN = 2;

ZEROS

Specify the fill mode as ZEROS

1
const crypto.ZEROS = 3;

NOPADDING

Specify the filling mode as NOPADDING

1
const crypto.NOPADDING = 4;