Python中各种加密模块的应用是怎样的
Admin 2022-08-18 群英技术资讯 746 次浏览
这篇文章主要介绍了Python中各种加密模块的应用是怎样的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Python中各种加密模块的应用是怎样的文章都会有所收获,下面我们一起来看看吧。它包含所有配方和基元,并在Python中提供高级编码接口.您可以使用以下命令安装加密模块 :
pip install cryptography

您可以使用以下代码实现加密模块 :
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt("This example is used to demonstrate cryptography module")
plain_text = cipher_suite.decrypt(cipher_text)
上面给出的代码产生以下输出 :

此处给出的代码用于验证密码并创建其哈希值.
它还包括用于验证密码以进行身份验证的逻辑.
import uuid
import hashlib
def hash_password(password):
# uuid is used to generate a random number of the specified password
salt = uuid.uuid4().hex
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
def check_password(hashed_password, user_password):
password, salt = hashed_password.split(':')
return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
new_pass = input('Please enter a password: ')
hashed_password = hash_password(new_pass)
print('The string to store in the db is: ' + hashed_password)
old_pass = input('Now please enter the password again to check: ')
if check_password(hashed_password, old_pass):
print('You entered the right password')
else:
print('Passwords do not match')
输出
场景1 : 如果您输入了正确的密码,您可以找到以下输出 :

情景2 : 如果我们输入错误的密码,您可以找到以下输出 :

Hashlib 包用于在数据库中存储密码.在此程序中,使用 salt ,在实现哈希函数之前,将随机序列添加到密码字符串中.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
轮廓可以简单认为成将连续的点(连着边界)连在一起的曲线,具有相同 的颜色或者灰度。轮廓在形状分析和物体的检测和识别中很有用。
Python 3 的标准库中没多少用来解决加密的,不过却有用于处理哈希的库。在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的
在pandas中,当经常对数据进行处理,可能会造成数据索引顺序混乱,那么也就会影响数据读取、插入等等操作,因此重置索引的操作就很重要,那么pandas中重置索引怎样做?接下来给大家分享几个方法,大家可以参考。
这篇文章主要介绍了Python写一个简单上课点名系统,文章围绕Python得性概念资料写一个简的得上课点名系统,并附上详细的代码即过程总结,需要的朋友可以参考一下,希望对你有所帮助
装饰器接收一个功能,添加一些功能并返回。 在本文中,您将学习如何创建装饰器,以及为什么要使用装饰器。Python有一个有趣的功能,称为装饰器,以便为现有代码添加功能。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008