NODE. JS Encryption module Crypto commonly used methods introduced _node.js

Source: Internet
Author: User
Tags base64 hmac md5 openssl openssl version sha1 sha1 encryption

Use require (' crypto ') to invoke the cryptographic module.

The encryption module requires the support of the underlying system to provide OpenSSL. It provides a way to encapsulate security credentials and can be used for HTTPS secure networks and normal HTTP connections.

The module also provides a package for OpenSSL hash (hash), HMAC (key hash), cipher (encoding), decipher (decoding), sign (signature), and verify (authentication) methods.

Crypto.createcredentials (Details)

Create a voucher object with the optional parameter details as a dictionary with a key value:
Key: Is a string, PEM-encoded private key.
Cert: A string, PEM-encoded authentication certificate.
CA: PEM-encoded trusted CA certificate in string form, or certificate list.

If the details of the ' CA ' are not given, then Node.js will use the default public trusted list, which is located in the http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/ Builtins/certdata.txt.

Crypto.createhash (algorithm)

Creates and returns a hash object that is an encrypted hash of a specified algorithm used to generate a hash summary.

Parameter algorithm can select the algorithm supported by the OpenSSL version installed on the system. For example: ' SHA1 ', ' MD5 ', ' sha256 ', ' sha512 ' and so on. In a recently released release, OpenSSL LIST-MESSAGE-DIGEST-ALGORITHMS displays these available digest algorithms.

Hash.update (data)

Update the contents of the hash for the specified data. This method may be called more than once when the stream data is being used.

Hash.digest (encoding= ' binary ')

Calculates a hash summary of all incoming data. Parameter encoding (encoding method) can be ' hex ', ' binary ' or ' base64 '.

Crypto.createhmac (algorithm, key)

Creates and returns an Hmac object, which is an encrypted HMAC that specifies the algorithm and key.

Parameter algorithm can select the OpenSSL-supported algorithm-see createhash above. Parameter key is the key used by HMAC.

Hmac.update (data)

Updates the contents of the HMAC to the specified data. This method may be called more than once when the stream data is being used.

Hmac.digest (encoding= ' binary ')

The HMAC summary that calculates all incoming data. Parameter encoding (encoding method) can be ' hex ', ' binary ' or ' base64 '.

Crypto.createcipher (algorithm, key)

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

Parameter algorithm can select OpenSSL supported algorithms, such as ' aes192 ' and so on. In the most recent release, OpenSSL LIST-CIPHER-ALGORITHMS displays the encryption algorithm available.

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

Use parameter data to update the content to be encrypted, which is encoded by the parameter input_encoding and 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 the encrypted content that may be called multiple times when the stream data is used.

Cipher.final (output_encoding= ' binary ')

Returns all the remaining encrypted content, output_encoding output encoded as ' binary ', ' ascii ' or ' utf8 ' one of them.

Crypto.createdecipher (algorithm, key)

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

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

Use parameter data to update the content to decrypt, which is encoded in ' binary ', ' base64 ' or ' hex '. The parameter output_encoding specifies the output encoding of the decrypted plaintext content, either ' binary ', ' ascii ' or ' UTF8 '.

Decipher.final (output_encoding= ' binary ')

Returns all remaining decrypted plaintext, which is output_encoding ' binary ', ' ascii ' or ' UTF8 '.

Crypto.createsign (algorithm)

Creates and returns a Signer object using the given algorithm. In an existing OpenSSL release, OpenSSL LIST-PUBLIC-KEY-ALGORITHMS displays the available signature algorithms, such as: ' rsa-sha256 '.

Signer.update (data)

Updates the signer object with the data parameter. This method may be called more than once when the stream data is being used.

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

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

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 signer object above.

Verifier.update (data)

Updates the validator object with the data parameter. This method may be called more than once when the stream data is being used.

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

Validate signed data using parameter cert and signature, cert for PEM-encoded public key strings, signature for signatures of previously computed data, Signature_format can be ' binary ', ' hex ' or ' base64 ' '。

Returns TRUE or false based on the results of the signature validation of the data and public key.

When you need an irreversible encryption code how to write

Copy Code code as follows:

var text = "123|12312312123123121231231212312312123123121231231212312312";
var hasher=crypto.createhash ("MD5");
Hasher.update (text);
var hashmsg=hasher.digest (' hex ');//hashmsg to data after encryption

When you need an environment for encryption and decryption

Copy Code code as follows:

var key= "asdhjwheru*asd123-123";/Encrypted secret key
var text = "123|12312312123123121231231212312312123123121231231212312312";
var crypted =cipher.update (text, ' UTF8 ', ' hex ');
Crypted+=cipher.final (' hex ');
Value after the Var message=crypted;//encryption
var decipher = crypto.createdecipher (' AES-256-CBC ', key);
var dec=decipher.update (message, ' hex ', ' UTF8 ');
dec+= decipher.final (' UTF8 '), value after decryption

PS: About encryption technology, the site also provides the following encryption tools for your reference to use:

MD5 Online encryption tool: Http://tools.jb51.net/password/CreateMD5Password

Escape Encryption/decryption tool: http://tools.jb51.net/password/escapepwd

Online SHA1 encryption tool: Http://tools.jb51.net/password/sha1encode

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

short link (short URL) online Restore tool: Http://tools.jb51.net/password/unshorturl

High Strength password generator: Http://tools.jb51.net/password/CreateStrongPassword

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.