Module basic module

module hash

The hash module is mainly used to implement message digest algorithms and is a very important sub-module in implementing encryption algorithms. At the same time, the message digest algorithm is also the basis of many encryption algorithms.

Before using hasha submodule, you need to require('hash');load the module with .

In hashthe module, it mainly includes the following classes Digest, Hmac. Among them, Digestthe class is used to calculate the message digest algorithm, and Hmacthe class is used to calculate the keyed hash message.

Here is a simple example code using Digestthe class Compute MD5summary:

1 2 3 4 5 6 7 8 9 10 11 12
const hash = require('hash'); // create a MD5 digest object const digest = hash.digest(hash.MD5); // update digest with data const data = 'hello, world'; digest.update(data); // output result in hex const result = digest.digest('hex'); console.log(result);

In the above example code, we first initialize hash.digest([hash.MD5](hash.md#MD5))an Digestobject to MD5the pattern using . We then use update()the method to add the data required for digest calculation to the Digest object. Next, we use digest()the method to calculate the summary result and return the encoding specified by the parameter (hexadecimal encoding is used here). Finally, the calculated summary results are output.

In MD5addition to , Digestthe class also supports other summary algorithms, such as SHA-1, SHA-256, SHA-512etc. When using it, you need to md5change the parameters to the corresponding summary algorithm.

static function

digest

Creates an Information Digest operand based on the specified algorithm identifier.

1 2
static Digest hash.digest(Integer algo, Buffer data = NULL);

Call parameters:

  • algo: Integer, specify the digest operation algorithm
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

md5

Create an MD5 message digest operand

1
static Digest hash.md5(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha1

Create a SHA1 message digest operand

1
static Digest hash.sha1(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha224

Create a SHA224 message digest operand

1
static Digest hash.sha224(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha256

Create a SHA256 message digest operand

1
static Digest hash.sha256(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha384

Create a SHA384 message digest operand

1
static Digest hash.sha384(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha512

Create a SHA512 message digest operand

1
static Digest hash.sha512(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

ripemd160

Create a RIPEMD160 message digest operand

1
static Digest hash.ripemd160(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sm3

Create an SM3 message digest operand

1
static Digest hash.sm3(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

Create an SM3 message digest operand and preprocess it

1 2 3
static Digest hash.sm3(PKey pubKey, String id, Buffer data = NULL);

Call parameters:

  • pubKey:PKey, signature public key
  • id: String, signature ID
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha3_256

Create a SHA3_256 message digest operand

1
static Digest hash.sha3_256(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha3_384

Create a SHA3_384 message digest operand

1
static Digest hash.sha3_384(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

sha3_512

Create a SHA3_512 message digest operand

1
static Digest hash.sha3_512(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

shake128

Create a SHAKE128 message digest operand

1
static Digest hash.shake128(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

shake256

Create a SHAKE256 message digest operand

1
static Digest hash.shake256(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

keccak256

Create a KECCAK256 message digest operand

1
static Digest hash.keccak256(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

keccak384

Create a KECCAK384 message digest operand

1
static Digest hash.keccak384(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

keccak512

Create a KECCAK512 message digest operand

1
static Digest hash.keccak512(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

blake2s

Create a BLAKE2S message digest operand

1
static Digest hash.blake2s(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

blake2b

Create a BLAKE2B message digest operand

1
static Digest hash.blake2b(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

blake2sp

Create a BLAKE2SP message digest operand

1
static Digest hash.blake2sp(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

blake2bp

Create a BLAKE2BP message digest operand

1
static Digest hash.blake2bp(Buffer data = NULL);

Call parameters:

  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac

Creates a message digest signature operand based on the specified algorithm identifier.

1 2 3
static Digest hash.hmac(Integer algo, Buffer key, Buffer data = NULL);

Call parameters:

  • algo: Integer, specify the digest operation algorithm
  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_md5

Create an MD5 message digest signature operand

1 2
static Digest hash.hmac_md5(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha1

Create a SHA1 message digest signature operand

1 2
static Digest hash.hmac_sha1(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha224

Create a SHA224 message digest signature operand

1 2
static Digest hash.hmac_sha224(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha256

Create a SHA256 message digest signature operand

1 2
static Digest hash.hmac_sha256(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha384

Create a SHA384 message digest signature operand

1 2
static Digest hash.hmac_sha384(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha512

Create a SHA512 message digest signature operand

1 2
static Digest hash.hmac_sha512(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_ripemd160

Create a RIPEMD160 message digest signature operation object

1 2
static Digest hash.hmac_ripemd160(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sm3

Create an SM3 message digest signature operand

1 2
static Digest hash.hmac_sm3(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

Create an SM3 message digest signature operation object and perform preprocessing

1 2 3 4
static Digest hash.hmac_sm3(PKey pubKey, String id, Buffer key, Buffer data = NULL);

Call parameters:

  • pubKey:PKey, signature public key
  • id: String, signature ID
  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha3_256

Create a SHA3_256 message digest signature operand

1 2
static Digest hash.hmac_sha3_256(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha3_384

Create a SHA3_384 message digest signature operand

1 2
static Digest hash.hmac_sha3_384(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_shake128

Create a SHAKE128 message digest signature operation object

1 2
static Digest hash.hmac_shake128(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_shake256

Create a SHAKE256 message digest signature operation object

1 2
static Digest hash.hmac_shake256(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_sha3_512

Create a SHA3_512 message digest signature operand

1 2
static Digest hash.hmac_sha3_512(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_keccak256

Create a KECCAK256 message digest signature operand

1 2
static Digest hash.hmac_keccak256(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_keccak384

Create a KECCAK384 message digest signature operand

1 2
static Digest hash.hmac_keccak384(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_keccak512

Create a KECCAK512 message digest signature operand

1 2
static Digest hash.hmac_keccak512(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_blake2s

Create a BLAKE2S message digest signature operation object

1 2
static Digest hash.hmac_blake2s(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_blake2b

Create a BLAKE2B message digest signature operation object

1 2
static Digest hash.hmac_blake2b(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_blake2sp

Create a BLAKE2SP message digest signature operation object

1 2
static Digest hash.hmac_blake2sp(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

hmac_blake2bp

Create a BLAKE2BP message digest signature operation object

1 2
static Digest hash.hmac_blake2bp(Buffer key, Buffer data = NULL);

Call parameters:

  • key:Buffer, binary signing key
  • data:Buffer, create binary data that is updated at the same time. The default is null and the data is not updated.

Return results:

  • Digest, returns the constructed information summary object

constant

MD5

MD5 message digest algorithm identification constant

1
const hash.MD5 = 1;

SHA1

SHA1 message digest algorithm identification constant

1
const hash.SHA1 = 2;

SHA224

SHA224 message digest algorithm identification constant

1
const hash.SHA224 = 3;

SHA256

SHA256 message digest algorithm identification constant

1
const hash.SHA256 = 4;

SHA384

SHA384 message digest algorithm identification constant

1
const hash.SHA384 = 5;

SHA512

SHA512 message digest algorithm identification constant

1
const hash.SHA512 = 6;

RIPEMD160

RIPEMD160 information digest algorithm identification constant

1
const hash.RIPEMD160 = 7;

SM3

SM3 message digest algorithm identification constants

1
const hash.SM3 = 8;

SHA3_256

SHA3_256 message digest algorithm identification constant

1
const hash.SHA3_256 = 9;

SHA3_384

SHA3_384 message digest algorithm identification constant

1
const hash.SHA3_384 = 10;

SHA3_512

SHA3_512 message digest algorithm identification constant

1
const hash.SHA3_512 = 11;

SHAKE128

SHAKE128 message digest algorithm identification constant

1
const hash.SHAKE128 = 12;

SHAKE256

SHAKE256 message digest algorithm identification constant

1
const hash.SHAKE256 = 13;

KECCAK256

KECCAK256 Information digest algorithm identification constant

1
const hash.KECCAK256 = 14;

KECCAK384

KECCAK384 Information digest algorithm identification constant

1
const hash.KECCAK384 = 15;

KECCAK512

KECCAK512 Information digest algorithm identification constant

1
const hash.KECCAK512 = 16;

BLAKE2S

BLAKE2S message digest algorithm identification constants

1
const hash.BLAKE2S = 17;

BLAKE2B

BLAKE2B message digest algorithm identification constants

1
const hash.BLAKE2B = 18;

BLAKE2SP

BLAKE2SP message digest algorithm identification constants

1
const hash.BLAKE2SP = 19;

BLAKE2BP

BLAKE2BP message digest algorithm identification constant

1
const hash.BLAKE2BP = 20;