当前位置:网站首页>关于密码加密的一点思路
关于密码加密的一点思路
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'))
边栏推荐
猜你喜欢

你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27

关于市场后市的发展预测? 2021-05-23

监管再次重拳出击,后市如何?2021-05-22

Differences and concepts between software testing and hardware testing

鲲鹏devkit & boostkit

The future of financial services will never stop, and the bull market will continue 2021-05-28

Sentinel源码(三)slot解析

此次519暴跌的几点感触 2021-05-21

方舟生存进化淘宝面板服务器是怎么一回事?

The most complete ever!A collection of 47 common terms of "digital transformation", read it in seconds~
随机推荐
ttl电平与rs232电平转换电路(232电平定义)
tinymce 如何实现动态国际化
如何解决mysql服务无法启动1069
期货具体是如何开户的?
面试官:可以谈谈乐观锁和悲观锁吗
Large and comprehensive pom file example
FFmpeg AVPacket详解
你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27
网络安全第一次作业
replay视频播放器_怎么让手机音乐跟视频一起放
RKMPP 在FFmpeg上实现硬编解码
标量替换、栈上分配、同步消除
专访|带着问题去学习,Apache DolphinScheduler 王福政
社区收藏缓存设计重构实战
idea社区版下载安装教程_安装天然气管道的流程
C language improvement (3)
Minio文件上传
腾讯安全游戏行业研讨会:生态共建,护航游戏产业健康发展
劲爆!阿里巴巴面试参考指南(嵩山版)开源分享,程序员面试必刷
Sentinel源码(一)SentinelResourceAspect