当前位置:网站首页>A little thought about password encryption
A little thought about password encryption
2022-08-02 14:21:00 【Spaghetti Mixed with No. 42 Concrete】
A little thought on password encryption
How in the case that the database is leaked by the hijacking code,The user password will still not be cracked
With this question in mind, I wrote some code
from hashlib import sha256, sha512
test_pwd = 'testpassword123'
class CipherHandler(object):
""" Password encryption and password comparison """
# 密码盐
salt = "cvbjd%*vfjkse#fuei"
@classmethod
def encrypt(cls, pwd: str) -> str:
""" Randomly add salt first,use it twicesha256/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'))
边栏推荐
- 理解TCP长连接(Keepalive)
- yolov5 improvement (1) Add attention focus mechanism
- 网页设计(新手入门)[通俗易懂]
- Sentinel源码(五)FlowSlot以及限流控制器源码分析
- Mysql's case the when you how to use
- verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十章)
- [ROS] (06) ROS Communication - Topic Communication
- xshell连接虚拟机步骤_建立主机与vm虚拟机的网络连接
- paddleocr window10初体验
- Implementation of redis distributed lock and watchdog
猜你喜欢

chapter6可视化(不想看版)

MarkDown语法汇总

verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十二章)

run yolov5

window10下半自动标注

The bad policy has no long-term impact on the market, and the bull market will continue 2021-05-19

MobileNet ShuffleNet & yolov5 replace backbone
![[ROS] Introduction to common tools in ROS (to be continued)](/img/ea/e390106f750bf697e62a3a296014d2.png)
[ROS] Introduction to common tools in ROS (to be continued)

瑞吉外卖笔记——第10讲Swagger
创建&编译ROS软件包Package](/img/c2/5931d5cbade509c6ca34d66a274756.png)
[ROS](02)创建&编译ROS软件包Package
随机推荐
Error Correction Design Principle of Hamming Check Code
跑跑yolov5吧
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十章)
第十二单元 关联序列化处理
Unit 5 Hold Status
[ROS](01)创建ROS工作空间
paddle window10环境下使用conda安装
jwt(json web token)
第十三单元 混入视图基类
VMM是什么?_兮是什么意思
Deep learning framework pytorch rapid development and actual combat chapter3
Steps to connect the virtual machine with xshell_establish a network connection between the host and the vm virtual machine
Data Organization---Chapter 6 Diagram---Graph Traversal---Multiple Choice Questions
paddleocr window10 first experience
Deep learning framework pytorch rapid development and actual combat chapter4
一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
Unit 3 view layer
Unit 11 Serializers
【VCU】详解S19文件(S-record)
瑞吉外卖笔记——第05讲Redis入门