Detailed description of encryption operations in nodejs and nodejs
This document describes how to encrypt passwords in nodejs. We will share this with you for your reference. The details are as follows:
I. Aboutnode
Encryption modulecrypto
Introduction
It is actually encrypted using MD5, which is not secure. In actual development, you can add salt according to your own solution.
2. Use encryption in the routing View
1. Import the built-in encryption module of node (no installation is required)
// Import the encryption module const crypto = require ("crypto ");
2. Create a user registration and password encryption View
<Div class = "col-md-6">
Router. post ("/regest", (req, res) => {console. log (req. body); let name = req. body. username; let password = req. body. password; let md5 = crypto. createHash ("md5"); let newPas = md5.update (password ). digest ("hex"); db ("insert into user1 (name, password) values (?,?) ", [Name, newPas], (err, data) =>{ if (err) {res. send ("registration failed");} console. log (data); if (data) {res. send ("registered successfully ");}})});
3. User Login for password verification
1. encrypt the password entered by the user in the same way
2. Match the encrypted password with the database
Router. post ("/login", (req, res) => {let name = req. body. username; let password = req. body. password; let md5 = crypto. createHash ("md5"); let newPas = md5.update (password ). digest ("hex"); db ("select * from user1 where name =? ", [Name], (err, data) => {console. log (data [0]. password); if (err) {res. send ("error");} if (data) {if (data [0]. password = newPas) {res. send ("Logon successful");} else {res. send ("incorrect user name or password ");}}})})
<Div class = "col-md-6">
Iv. Expansion (encryption is generally used)
1. How many digits are randomly generated using random numbers?
2. Use reversible encryption to encrypt the random number generated in step 1
Reversible EncryptionBase64
AndHex
Encryption (Baidu)
3. splice the random number encrypted in step 2 with our real password.
4. encrypt the third step (MD5
)
5. Reversible Encryption of Step 4
6. splice the data generated in step 2 and Step 5 into a password.
5. Expansion (usually encrypted login)
1. Get the password upon login
2. Extract the random number encrypted section from the obtained Password
3. Repeat the preceding encryption method)
6. Code for developing encryption methods in normal projectsCorrect Encryption Method
PS: if you are interested in encryption and decryption, refer to the online tools on this site:
BASE64Encoding and decoding tools:
Http://tools.jb51.net/transcoding/base64
MD5Online encryption tools:
Http://tools.jb51.net/password/CreateMD5Password
Online text encryption and decryption tools (including AES, DES, and RC4 ):
Http://tools.jb51.net/password/txt_encode
Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode
I hope this article will help you design nodejs programs.