当前位置:网站首页>关于密码加密的一点思路
关于密码加密的一点思路
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'))
边栏推荐
- About the development forecast of the market outlook?2021-05-23
- Large and comprehensive pom file example
- How to solve 1045 cannot log in to mysql server
- 网络安全第一次作业(2)
- Diodes and their applications
- 理解TCP长连接(Keepalive)
- 线代:已知一个特征向量快速求另外两个与之正交的特征向量
- 【Tensorflow】AttributeError: module ‘keras.backend‘ has no attribute ‘tf‘
- The future of financial services will never stop, and the bull market will continue 2021-05-28
- MySQL数据库语法格式
猜你喜欢
随机推荐
玉溪卷烟厂通过正确选择时序数据库 轻松应对超万亿行数据
WeChat Mini Program-Recent Dynamic Scrolling Implementation
打破文件锁限制,以存储力量助力企业增长新动力
vim复制粘贴_vim如何复制粘贴
els 长条碰撞变形判断
大而全的pom文件示例
微信小程序-最近动态滚动实现
Mysql's case the when you how to use
Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush
【ONE·Data || Getting Started with Sorting】
微信小程序如何实现支付功能?看官方文档头疼(使用云函数的方式操作)「建议收藏」
els 长条方块变形条件、边界碰撞判定
如何自定义feign方法级别的超时时间
苏州大学:从 PostgreSQL 到 TDengine
VMM是什么?_兮是什么意思
好用的php空间,推荐国内三个优质的免费PHP空间[通俗易懂]
线代:已知一个特征向量快速求另外两个与之正交的特征向量
logback源码阅读(二)日志打印,自定义appender,encoder,pattern,converter
What is the difference between web testing and app testing?
replay视频播放器_怎么让手机音乐跟视频一起放









