Home  >  Article  >  Web Front-end  >  Detailed explanation of the steps for MD5 encryption of data through node.js

Detailed explanation of the steps for MD5 encryption of data through node.js

php中世界最好的语言
php中世界最好的语言Original
2018-05-29 13:54:562152browse

This time I will bring you a detailed explanation of the steps for MD5 encryption of data through node.js, and what are the precautions for encrypting data through MD5 through node.js, as follows This is a practical case, let’s take a look at it.

md5 introduction: MD5 is a commonly used hash algorithm, mainly used to "sign" some important data. Of course, these data can be arbitrary. The final "signature" is usually a 16 or 32-digit hexadecimal string.

In actual work development, no one will directly put the plain text of the password directly into the database. Because this approach is very unsafe, it is generally required to be MD5 encrypted! For example, a user's password is "123456" (of course, such a password has no security). After MD5 processing, it becomes:

"e10adc3949ba59abbe56e057f20f883e". The main benefits of doing this are the following two points:

1. PreventionWebsite Operation Insiders know your password and do some unethical behavior. Because many people set all their passwords to the same for convenience.

2. Prevent external attacks. If the website you registered is attacked by a hacker and you get some data from the website, you will also get a bunch of strings processed by MD5.

Note: MD5 generally cannot be decompiled. The premise is that your password should not be set too simple. This is why more and more websites now have higher and higher requirements for the passwords set by users. Not only must the password be long enough, but the password must also include numbers, large and small letters, and special characters. Character string.

This article will mainly focus on how to encrypt data with MD5 in NODE.JS:

Introduce the md5 dependency package directly into NODE:

Download and install md5 dependency package

npm install md5

md5 encryption example:

var md5 =require("md5");
//设置加密字符串
var passWord="if(1==1){console.log('i love you')}";
console.log(md5(passWord));//a775657889f1ad6e19178c3cd734392b

Of course, simply encrypting data with MD5 is not safe. , it’s best to take the data with a pinch of salt:

var md5 =require("md5");
//设置加密字符串
var passWord="if(1==1){console.log('i love you')}";
//在原来的字符串的基础上加上一些特殊文本,例如“zhangpeiyue.com"
console.log(md5(passWord+"zhangpeiyue.com"));//13c22698f52329433107e75b49330484

MD5 encryption method in nodejs:

First one:

var crypto=require('crypto'); 
var md5=crypto.createHash("md5"); 
md5.update("abcdef"); 
var str=md5.digest('hex'); 
var s=str.toUpperCase(); //32位大写 
console.log(s);

Second type:

var md5=require("md5") 
var str=md5("abcdef") 
//str=e80b5017098950fc58aad83c8c14978e 默认32位小写

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use Vue to make Amap and build a real-time bus application

How to use seajs in require Writing convention

The above is the detailed content of Detailed explanation of the steps for MD5 encryption of data through node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn