当前位置:网站首页>关于密码加密的一点思路
关于密码加密的一点思路
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'))
边栏推荐
- 文件加密软件有哪些?保障你的文件安全
- RKMPP 在FFmpeg上实现硬编解码
- The future of financial services will never stop, and the bull market will continue 2021-05-28
- 方舟生存进化淘宝面板服务器是怎么一回事?
- 网络安全第二次作业
- Interview | with questions to learn, Apache DolphinScheduler Wang Fuzheng
- How to solve 1045 cannot log in to mysql server
- 【学习笔记】数位dp
- 好用的php空间,推荐国内三个优质的免费PHP空间[通俗易懂]
- logback源码阅读(一)获取ILoggerFactory、Logger
猜你喜欢
logback源码阅读(一)获取ILoggerFactory、Logger
RKMPP 在FFmpeg上实现硬编解码
【ONE·Data || Getting Started with Sorting】
不精确微分/不完全微分(Inexact differential/Imperfect differential)
栈 && 队列
uview 2.x版本 tabbar在uniapp小程序里头点击两次才能选中图标
binary search && tree
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
数据机构---第六章图---图的遍历---选择题
智能指针-使用、避坑和实现
随机推荐
ttl电平与rs232电平转换电路(232电平定义)
“多源异构”和“异构同源”定义区分详解「建议收藏」
如何解决mysql服务无法启动1069
网络安全第二次作业
uview 2.x版本 tabbar在uniapp小程序里头点击两次才能选中图标
Image retrieval method based on deep learning!
WeChat Mini Program-Recent Dynamic Scrolling Implementation
logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
智能指针-使用、避坑和实现
RKMPP库快速上手--(一)RKMPP功能及使用详解
C语言提高篇(三)
Audio processing: floating point data stream to PCM file
Diodes and their applications
无序数组排序并得到最大间隔
Sentinel源码(四)(滑动窗口流量统计)
SQL函数 USER
idea社区版下载安装教程_安装天然气管道的流程
Word | 关于删除分节符(下一页)前面的版式就乱了解决方案
A number of embassies and consulates abroad have issued reminders about travel to China, personal and property safety
RKMPP API安装使用总结