Introduction to common methods of CRYPTO in node. JS encryption module

  • 2020-03-30 03:11:00
  • OfStack

The encryption module is invoked using require('crypto').

The module also provides a package of methods such as hash, hmac, cipher, decipher, sign, and verify for OpenSSL.

Crypto. CreateCredentials (details)

Create a credential object with the optional parameter details as a dictionary with key values:
Key: is a string private key encoded by PEM.
Cert: authentication certificate of string type, encoded by PEM.
Ca: PEM encodes a trusted ca certificate as a string, or a list of certificates.

If there is no 'ca' details are given, then the node. Js will use the default public trusted list, the table at http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt.

Crypto. CreateHash (algorithm)

Creates and returns a hash object, an encrypted hash of the specified algorithm, used to generate a hash digest.

Hash. Update (data)

Updates the contents of the hash to the specified data. This method may be called multiple times when using stream data.

= 'binary' hash digest (encoding)

Calculate the hash digest for all incoming data. The parameter encoding can be 'hex', 'binary', or 'base64'.

Crypto. CreateHmac (algorithm, key)

Creates and returns an hmac object, which is an encrypted hmac specifying the algorithm and key.

Hmac. Update (data)

Updates the hmac to the specified data. This method may be called multiple times when using stream data.

Hmac. Digest (encoding = 'binary')

Calculate the hmac digest for all incoming data. The parameter encoding can be 'hex', 'binary', or 'base64'.

Crypto. CreateCipher (algorithm, key)

Creates and returns a cipher object using the specified algorithm and key.

Cipher. Update (data, input_encoding = 'binary', output_encoding = 'binary')

Update the contents to be encrypted with the parameter data, encoded as specified by the parameter input_encoding, which can be 'utf8', 'ASCII ', or 'binary'. The parameter output_encoding specifies the output encoding of the encrypted content, which can be 'binary', 'base64', or 'hex'.

Returns encrypted content, which may be called multiple times when using stream data.

Cipher. Final (output_encoding = 'binary')

Returns all remaining encrypted contents, one of which output_encoding outputs encoded 'binary', 'ASCII' or 'utf8'.

Crypto. CreateDecipher (algorithm, key)

Creates and returns a decryption object using the given algorithm and key. This object is the reverse operation of the above encrypted object.

Decipher. Update (data, input_encoding = 'binary', output_encoding = 'binary')

Update the contents to be decrypted using the parameter data, encoded as 'binary', 'base64' or 'hex'. The parameter output_encoding specifies the output encoding of the decrypted plaintext content, which can be 'binary', 'ASCII', or 'utf8'.

Decipher. Final (output_encoding = 'binary')

Returns all remaining decrypted plaintext whose output_encoding' is 'binary', 'ASCII' or 'utf8' '.

Crypto. CreateSign (algorithm)

Creates and returns a signature object using the given algorithm. In existing OpenSSL distributions, the OpenSSL list-public-key-algorithms show the signature algorithms available, such as' rsa-sha256 '.

Signer. Update (data)

Update the signature object with the data parameter. This method may be called multiple times when using stream data.

Signer sign (private_key, output_format = 'binary')

Calculates the signature of all incoming data from the signer. Private_key is a string that contains the pem-encoded private key for the signature.

Returns a signature whose output_format output can be 'binary', 'hex', or 'base64'.

Crypto. CreateVerify (algorithm)

Creates and returns a validator object using the given algorithm. It is the reverse operation of the above signature object.

Verifier update (data)

Update the validator object with the data parameter. This method may be called multiple times when using stream data.

Verifier. Verify (cert, signature, signature_format = 'binary')

Verify the signed data with the parameters cert and signature, cert as a pem-encoded public key string, signature as the signature of previously computed data, and signature_format as 'binary', 'hex', or 'base64'.

Returns true or false based on the result of the signature validation of the data and public key.

When you need an irreversible encryption code how to write


var text = "123|12312312123123121231231212312312123123121231231212312312";
var hasher=crypto.createHash("md5");
hasher.update(text);
var hashmsg=hasher.digest('hex');//hashmsg Is the encrypted data

When you need an environment for encryption and decryption


var key="asdhjwheru*asd123-123";//The encrypted secret key
var text = "123|12312312123123121231231212312312123123121231231212312312";
var crypted =cipher.update(text,'utf8','hex');
crypted+=cipher.final('hex');
var message=crypted;//The encrypted value
var decipher = crypto.createDecipher('aes-256-cbc',key);
var dec=decipher.update(message,'hex','utf8');
dec+= decipher.final('utf8');// Decrypted values

PS: about encryption technology, this site also provides the following encryption tools for your reference:

MD5 online encryption tool: (link: http://tools.jb51.net/password/CreateMD5Password)

Escape encryption/decryption tool: (link: http://tools.jb51.net/password/escapepwd)

Online SHA1 encryption tool: (link: http://tools.jb51.net/password/sha1encode)

Short chain (short url) online generation tool: (link: http://tools.jb51.net/password/dwzcreate)

Short chain (short url) online restore tool: (link: http://tools.jb51.net/password/unshorturl)

High-strength password generator: (link: http://tools.jb51.net/password/CreateStrongPassword)


Related articles: