当前位置:网站首页>Node实现数据加密
Node实现数据加密
2022-07-31 14:33:00 【THER1881】
一、Node实现数据加密
1、加密的分类
(1)、对称加密:也称为单密钥加密.同一个密钥进行加密和解密
(2)、非对称加密:有两把钥匙(公钥和私钥)
(3)、摘要算法:把任意长度的输入,根据算法生成一串固定长度的伪随机数(没有密钥,加密过程是不可逆的)
2、MD5(摘要算法)在Node的使用方法
2.1、安装crypto模块
npm install crypto
2.2、使用crypto.createHash(‘md5’)创建加密对象
2.3、使用加密对象的update(明文)进行加密,然后调用digest(‘hex’)返回定长的十六进制字符串
3、演示:对用户的注册密码进行加密后存入数据库
3.1、安装模块
3.2、创建数据库连接配置对象的文件
//1、导入squelize模块
const Sequelize = require('sequelize')
//2、配置数据库连接对象
const sqllize =new Sequelize('mvc','root','qazzaq123',{
host:'localhost',
post:3306,
dialect:'mysql',
pool:{
//数据库连接池
max:10,
min:3,
idle:10000
}
})
//3、导出数据库的配置对象
module.exports = sqllize
3.3、创建模型
const Sequelize = require('sequelize')
const dqldemo = require('../seq')
const AdminModel = dqldemo.define('admins', {
Id: {
type: Sequelize.INTEGER,
primaryKey: true, // 主键
autoIncrement: true, //自增
field: 'id' //对应列名
},
userName: {
type: Sequelize.STRING,
allowNull: false, //不能为空
field: 'username'
},
userPwd: {
type: Sequelize.STRING,
field: 'userpassword'
}
},
{
freezeTableName:true,
timestamps:false
}
)
module.exports = AdminModel
3.4、进行crud操作
const express = require('express')
var crypto = require('crypto')
var adminmodel = require('../config/model/admin')
var router = express.Router()
//http://localhost:3000/md5/md5test
router.post('/md5test',(req, res) => {
let name =req.body.username
let Pwd = req.body.pwd
//对密码进行加密
//生成加密对象
let hash = crypto.createHash('md5')
//使用加密对象进行加密并生成十六进制字符串
let newPwd = hash.update(Pwd).digest('hex')
//将用户名和加密的密码保存到数据库中
adminmodel.create({
userName:name,
userPwd:newPwd
}).then((result)=>{
res.json({
code:1001,
msg:'保存成功'
})
}).catch((err)=>{
console.log(err)
res.json({
code:1002,
msg:'保存失败'
})
})
})
module.exports = router
3.5、配置app.js文件
var md5Router = require('./routes/md5')
app.use('/md5',md5Router)
3.6、测试
首先在apipost中进行测试:
在数据库中,也可以看到密码加密信息保存:
3.7、验证用户是否可以登录
const express = require('express')
var crypto = require('crypto')
var adminmodel = require('../config/model/admin')
var router = express.Router()
//http://localhost:3000/md5/md5login
router.post('/md5login',(req, res) => {
let name = req.body.username
let pwd =req.body.pwd
let hash = crypto.createHash('md5')
//使用加密对象进行加密并生成十六进制字符串
let newPwd = hash.update(pwd).digest('hex')
adminmodel.findOne({
where:{
userName:name
}
}).then((admin)=>{
if(admin.userPwd === newPwd){
res.json({
code:1001,
msg:'验证成功'
})
}else{
res.json({
code:1002,
msg:'密码错误,验证失败'
})
}
})
})
module.exports = router
3.8、前后端分离原则,所以需要写前端页面,进行前后端交互
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
div{
width: 500px;
margin: 100px auto;
}
</style>
<body>
<div>
<!-- <form action="" method="post"> -->
<label>
用户名:<input type="text" id="userName" name="username">
</label>
<br><br>
<label>
密 码:<input type="password" id="userPwd" name="pwd">
</label>
<br><br>
<button type="button" id="btn_reset">重置</button>
<button type="button" id="btn_submit">提交</button>
<!-- </form> -->
<br><br>
<span id="msg"></span>
</div>
<script src="./js/jquery-3.4.1.js"></script>
<script>
$(function(){
//jQuery入口函数
//1、给登录按钮绑定click事件
$('#btn_submit').click(function(){
//2、获取用户输入的密码和用户名
let name=$('#userName').val()
let pwd = $('#userPwd').val()
//3.调用ajax函数向服务器发送异步的请求
$.post('http://localhost:3000/md5/md5login',{
username:name,pwd:pwd},function(result){
$('#msg').html(result.msg)
},'json')
})
})
</script>
</body>
</html>
输入此前存入的用户信息和密码,显示验证成功
边栏推荐
- 2021 OWASP TOP 10 漏洞指南
- Motion capture system for end-positioning control of flexible manipulators
- Why do we need to sub-library and sub-table?
- svn安装及使用(身体功能手册)
- 【Pytorch】F.softmax()方法说明
- The 232-layer 3D flash memory chip is here: the single-chip capacity is 2TB, and the transmission speed is increased by 50%
- el-tooltip的使用
- OAuth2:使用JWT令牌
- An article makes it clear!What is the difference and connection between database and data warehouse?
- Tortoise speed by "template"
猜你喜欢
AWS implements scheduled tasks - Lambda+EventBridge
小试牛刀:Go 反射帮我把 Excel 转成 Struct
C# Get network card information NetworkInterface IPInterfaceProperties
Resnet&API
The JVM a class loader
OAuth2:资源服务器
Combination series - there are combinations when there are arrangements
Unity Shader入门精要学习——透明效果
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
消息队列消息数据存储MySQL表设计
随机推荐
多智能体协同控制研究中光学动作捕捉与UWB定位技术比较
[Pytorch] F.softmax() method description
BigDecimal 简介,常用方法
Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
C# using ComboBox control
Architecture actual combat battalion module 8 message queue table structure design
Groupid(artifact id)
MySQL玩到这种程度,难怪大厂抢着要!
R语言的画图代码及差异性分析[通俗易懂]
Redis 】 【 publish and subscribe message
NPM Taobao mirror (latest version) released a new version of npm mirror at 2021-11-21 16:53:52 [easy to understand]
DeepLab系列学习
【redis】发布和订阅消息
Linux bash: redis-server: 未找到命令
SetoolKit User Guide
Miller_Rabin Miller Rabin probability sieve [template]
Nuget打包并上传教程
Shell脚本经典案例:文件的备份
DeepLab Series Learning
AWS实现定时任务-Lambda+EventBridge