当前位置:网站首页>关于密码加密的一点思路
关于密码加密的一点思路
2022-08-02 14:01:00 【意大利面拌42号混凝土】
关于密码加密的一点思路
如何在数据库被劫持代码泄露的情况下,用户密码依然不会被破解呢
带着这个问题我书写了一下代码
from hashlib import sha256, sha512
test_pwd = 'testpassword123'
class CipherHandler(object):
""" 密码加密与密码比对 """
# 密码盐
salt = "cvbjd%*vfjkse#fuei"
@classmethod
def encrypt(cls, pwd: str) -> str:
""" 先随机加盐,再用两次sha256/sha512加密 :return: """
import random
randnum = random.randint(1, 3)
if randnum == 1:
salt_pwd = ''.join([cls.salt, pwd])
return cls._encryption(salt_pwd)
elif randnum == 2:
salt_pwd = ''.join([pwd, cls.salt])
return cls._encryption(salt_pwd)
elif randnum == 3:
pwd_list = list(pwd)
pwd_list.insert(randnum, cls.salt)
salt_pwd = ''.join(pwd_list)
return cls._encryption(salt_pwd)
@classmethod
def compare(cls, user_input_pwd: str,
database_storage_pwd: str
) -> bool:
""" 加密比对 :return: """
pwd_list = []
salt_pwd_one = ''.join([cls.salt, user_input_pwd])
pwd_list.append(cls._encryption(salt_pwd_one))
salt_pwd_two = ''.join([user_input_pwd, cls.salt])
pwd_list.append(cls._encryption(salt_pwd_two))
str_list = list(user_input_pwd)
str_list.insert(3, cls.salt)
salt_pwd_three = ''.join(str_list)
pwd_list.append(cls._encryption(salt_pwd_three))
if database_storage_pwd not in pwd_list:
return False
return True
@classmethod
def _encryption(cls, salt_pwd: str) -> str:
one_encryption = sha512((salt_pwd).encode('utf-8')).hexdigest()
enc_pwd = sha512((one_encryption).encode('utf-8')).hexdigest()
return enc_pwd
# 加密
print(CipherHandler.encrypt(test_pwd))
# 校验比对
print(CipherHandler.compare(test_pwd,'32b5b701ece1747eb90935fa55fe96983126a9683dbb1a537750fe5527eb3552ce13d31e2d2fad07fd8fe755794973221404e3a528dfa74ff0aff93e8b6745c6'))
边栏推荐
- FFmpeg AVPacket详解
- 软件测试和硬件测试的区别及概念
- SQL函数 UCASE
- 一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
- hsql是什么_MQL语言
- 乐心湖‘s Blog——MySQL入门到精通 —— 囊括 MySQL 入门 以及 SQL 语句优化 —— 索引原理 —— 性能分析 —— 存储引擎特点以及选择 —— 面试题
- You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
- Sentinel源码(六)ParamFlowSlot热点参数限流
- 理解TCP长连接(Keepalive)
- 面试官:可以谈谈乐观锁和悲观锁吗
猜你喜欢
如何解决1045无法登录mysql服务器
RKMPP 在FFmpeg上实现硬编解码
About the development forecast of the market outlook?2021-05-23
Raft协议图解,缺陷以及优化
如何解决mysql服务无法启动1069
Mysql's case the when you how to use
劲爆!阿里巴巴面试参考指南(嵩山版)开源分享,程序员面试必刷
未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28
如何选择正规的期货交易平台开户?
Break the limit of file locks and use storage power to help enterprises grow new momentum
随机推荐
一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
文件加密软件有哪些?保障你的文件安全
RKMPP库快速上手--(一)RKMPP功能及使用详解
拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢
【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
How to solve mysql service cannot start 1069
动态刷新日志级别
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
网络安全第六次作业
面试官:可以谈谈乐观锁和悲观锁吗
EasyExcel 的使用
云片网案例
Geoffery Hinton:深度学习的下一个大事件
SQL函数 $TSQL_NEWID
idea社区版下载安装教程_安装天然气管道的流程
xshell连接虚拟机步骤_建立主机与vm虚拟机的网络连接
数据机构---第六章图---图的遍历---选择题
GTK:Gdk-CRITICAL **: IA__gdk_draw_pixbuf: assertion ‘GDK_IS_DRAWABLE (drawable)‘ failed
定了!就在7月30日!
HALCON: 对象(object)从声明(declaration)到结束(finalization)