Use require('crypto') to call the encryption module.
The encryption module requires the support of OpenSSL from the underlying system. It provides a way to encapsulate security credentials, which can be used for HTTPS secure networks and ordinary HTTP connections.
The module also provides a set of encapsulations for OpenSSL's hash (hash), hmac (key hash), cipher (encoding), decipher (decoding), sign (sign), and verification (verification).
crypto.createCredentials(details)
Create a credential object with optional parameter details as a dictionary with key values:
key: a private key that is string-type, PEM-encoded.
cert: a string type, PEM encoded authentication certificate.
ca: A PEM encoded trusted CA certificate in string form, or a certificate list.
If no details of 'ca' are given, node.js will use the default public trusted list, which is located at http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt.
crypto.createHash(algorithm)
Creates and returns a hash object, which is an encrypted hash of the specified algorithm, used to generate a hash digest.
Parameter algorithm can select the algorithm supported by the OpenSSL version installed on the system. For example: 'sha1', 'md5', 'sha256', 'sha512', etc. In recent releases, openssl list-message-digest-algorithms displays these available digest algorithms.
hash.update(data)
Update the content of the hash to the specified data. This method may be called multiple times when using streaming data.
hash.digest(encoding='binary')
Computes the hash summary of all incoming data. The encoding parameter 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.
The parameter algorithm can be selected by OpenSSL-see createHash above. The parameter key is the key used by hmac.
hmac.update(data)
Update hmac content to the specified data. This method may be called multiple times when using streaming data.
hmac.digest(encoding='binary')
Computes the hmac digest of all incoming data. The encoding parameter can be 'hex', 'binary' or 'base64'.
crypto.createCipher(algorithm, key)
Create and return a cipher object using the specified algorithm and key.
Parameter algorithm can select algorithms supported by OpenSSL, such as 'aes192', etc. In recent releases, openssl list-cipher-algorithms displays the encryption algorithms available.
cipher.update(data, input_encoding='binary', output_encoding='binary')
Use parameter data to update the content to be encrypted, and its encoding method is specified by parameter input_encoding, which can be 'utf8', 'ascii' or 'binary'. The parameter output_encoding specifies the output encoding method of encrypted content, which can be 'binary', 'base64' or 'hex'.
Returns encrypted content, which may be called multiple times when using streaming data.
cipher.final(output_encoding='binary')
Returns all remaining encrypted content, the output_encoding output is encoded as one of 'binary', 'ascii' or 'utf8'.
crypto.createDecipher(algorithm, key)
Create and return a decrypted object using the given algorithm and key. This object is the reverse operation of the above-mentioned encrypted object.
decipher.update(data, input_encoding='binary', output_encoding='binary')
Use parameter data to update the content to be decrypted, and its encoding is 'binary', 'base64' or 'hex'. The parameter output_encoding specifies the output encoding method of the decrypted plaintext content, which can be 'binary', 'ascii' or 'utf8'.
decipher.final(output_encoding='binary')
Returns all remaining decrypted plaintexts, whose output_encoding' is one of 'binary', 'ascii' or 'utf8'`.
crypto.createSign(algorithm)
Create and return a signature object using the given algorithm. In existing OpenSSL distributions, openssl list-public-key-algorithms displays available signature algorithms, such as: 'RSA-SHA256'.
signer.update(data)
Update the signature object with the data parameter. This method may be called multiple times when using streaming data.
signer.sign(private_key, output_format='binary')
Calculate its signature for all data passed to the signature. private_key is a string that contains the PEM-encoded private key for signature.
Returns the signature, whose output_format output can be 'binary', 'hex' or 'base64'.
crypto.createVerify(algorithm)
Create and return 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 streaming data.
verify.verify(cert, signature, signature_format='binary')
Use the parameters cert and signature to verify signed data. cert is a PEM-encoded public key string, signature is the signature of previously calculated data, and signature_format can be 'binary', 'hex' or 'base64'.
Return true or false according to the results of the signature validity verification of the data and public key.
How to write an irreversible encryption code when you need
The code copy is as follows:
var text = "123|123123121231231212312312123123123121231231231231212312312312312312312312312312312312312312312
var hasher=crypto.createHash("md5");
hasher.update(text);
var hashmsg=hasher.digest('hex');//hashmsg is the encrypted data
When you need an encrypted and decrypted environment
The code copy is as follows:
var key="asdhjwhereu*asd123-123";//Encrypted secret key
var text = "123|123123121231231212312312123123123121231231231231212312312312312312312312312312312312312312312
var crypted =cipher.update(text,'utf8','hex');
crypted+=cipher.final('hex');
var message=crypted;//The value after encryption
var decipher = crypto.createDecipher('aes-256-cbc',key);
var dec=decipher.update(message,'hex','utf8');
dec+= decipher.final('utf8');//The value after decryption
PS: Regarding encryption technology, this site also provides the following encryption tools for your reference:
MD5 online encryption tool: http://tools.VeVB.COM/password/CreateMD5Password
Escape encryption/decryption tool: http://tools.VeVB.COM/password/escapepwd
Online SHA1 encryption tool: http://tools.VeVB.COM/password/sha1encode
Short link (short URL) online generation tool: http://tools.VeVB.COM/password/dwzcreate
Short link (short URL) online restore tool: http://tools.VeVB.COM/password/unshorturl
High-strength password generator: http://tools.VeVB.COM/password/CreateStrongPassword