欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

nodejs(5)crypto加密解密使用实例

程序员文章站 2022-04-12 23:33:37
const express = require("express");const app = express();const mysql = require("mysql");const crypto = require("crypto");app.listen(3000, function (err) { if (!err) { console.log("监听成功"); }});//链接数据库var db = mysql.createConnection({ host:...
const express = require("express");
const app = express();
const mysql = require("mysql");
const crypto = require("crypto");

app.listen(3000, function (err) {
  if (!err) {
    console.log("监听成功");
  }
});
//链接数据库
var db = mysql.createConnection({
  host: "**************",
  user: "root",
  password: "***",
  database: "****",
});

db.connect((err) => {
  if (!err) {
    console.log("数据库连接成功");
  }
  console.log(err);
});

app.post("/register", (req, res, next)=> {
  let username = req.body.username;
  let password = req.body.password;

  let md5 = crypto.createHash("md5");
  let newpass = md5.update(password).digest("hex");
  let sql = `insert into wangtinguser values('${username}','${newpass}')`;
  db.query(sql, (err, result) => {
    if (err) {
      console.log(err);
    } else {
      console.log("注册成功");
    }
  });
});

app.get('login',(req,res,next)=>{
    let username = req.query.username;
    let password = req.query.password;
  
    let md5 = crypto.createHash("md5");
    let newpass = md5.update(password).digest("hex");
    let sql = `select * from wangtinguser where 1=1 username = '${username}' and password ='${newpass}')`;
    db.query(sql, (err, result) => {
      if (err) {
        console.log(err);
      } else {
        console.log("登录成功");
      }
    });
})

本文地址:https://blog.csdn.net/weixin_46860184/article/details/108978748