Common Methods of NODE. JS encryption module CRYPTO _ node. js

Source: Internet
Author: User
Tags hmac key string openssl version
This article mainly introduces common methods of CRYPTO in the NODE. JS encryption module. For more information, see use require ('crypto') to call the encryption module.

The encryption module requires the underlying system to support OpenSSL. It provides a security credential encapsulation method that can be used for HTTPS security networks and common HTTP connections.

This module also provides a set of hash, hmac, cipher, decipher, sign, and verify for OpenSSL) and other methods.

Crypto. createCredentials (details)

Create a credential object. The optional parameter details is a dictionary with a key value:
Key: a string-type, PEM-encoded private key.
Cert: a string-type, PEM-encoded certificate.
Ca: a string-type PEM-encoded trusted CA certificate or certificate list.

If no details of 'CA' are provided, node. js uses the default public trusted list, which is located in the tables.

Crypto. createHash (algorithm)

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

The algorithm parameter selects the algorithms supported by the OpenSSL version installed on the system. For example, 'sha1', 'md5', 'sha256 ', and 'sha512. In recent releases, openssl list-message-digest-algorithms displays these available digest algorithms.

Hash. update (data)

The hash is updated to the specified data. This method may be called multiple times when streaming data is used.

Hash. digest (encoding = 'binary ')

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

Crypto. createHmac (algorithm, key)

Create and return an hmac object, which is an encrypted hmac with a specified algorithm and key.

You can select the algorithm supported by OpenSSL as the algorithm parameter. For more information, see createHash. The parameter key is the key used by hmac.

Hmac. update (data)

The hmac content is updated to the specified data. This method may be called multiple times when streaming data is used.

Hmac. digest (encoding = 'binary ')

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

Crypto. createCipher (algorithm, key)

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

The algorithm parameter can be used to select Algorithms supported by OpenSSL, such as 'aes192. In the latest release, openssl list-cipher-algorithms displays available encryption algorithms.

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

The parameter data is used to update the content to be encrypted. The encoding method is specified by the input_encoding parameter, which can be 'utf8', 'ascii ', or 'binary '. The output_encoding parameter specifies the output encoding method of the encrypted content, which can be 'binary ', 'base64', or 'hex '.

The encrypted content is returned. This method may be called multiple times when streaming data is used.

Cipher. final (output_encoding = 'binary ')

Returns all the remaining encrypted content. The output_encoding output is encoded as 'binary ', 'ascii', or 'utf8.

Crypto. createDecipher (algorithm, key)

Use the given algorithm and key to create and return a decryption object. This object is the reverse operation of the preceding encrypted object.

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

Use the parameter data to update the content to be decrypted. The encoding method is 'binary ', 'base64', or 'hex '. The output_encoding parameter specifies the output encoding method of decrypted plaintext content, which can be 'binary ', 'ascii', or 'utf8 '.

Decipher. final (output_encoding = 'binary ')

Return all the remaining decrypted plain text. output_encoding 'is one of the 'binary', 'ascii ', or 'utf8' values.

Crypto. createSign (algorithm)

Use the given algorithm to create and return a signature object. In the current OpenSSL release, openssl list-public-key-algorithms displays available signature algorithms, for example, 'rsa-SHA256 '.

Signer. update (data)

Use the data parameter to update the signature object. This method may be called multiple times when streaming data is used.

Signer. sign (private_key, output_format = 'binary ')

Calculate the signature for all data that are passed in the signature. Private_key is a string that contains the PEM-encoded private key for signature.

Return the signature. The output_format output can be 'binary ', 'hex', or 'base64 '.

Crypto. createVerify (algorithm)

Use the given algorithm to create and return a validator object. It is the inverse operation of the above signature object.

Verifier. update (data)

Use the data parameter to update the validator object. This method may be called multiple times when streaming data is used.

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

Use the cert and signature parameters to verify signed data. cert is a PEM-encoded public key string, signature is the signature of previously computed data, and signature_format can be 'binary ', 'hex' or 'base64 '.

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

How do you write an irreversible encryption code?

The Code is as follows:


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 encryption and decryption Environment

The Code is as follows:


Var key = "asdh1_heru * asd123-123"; // Encrypted 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 Value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.