Python中各种加密模块的应用是怎样的
Admin 2022-08-18 群英技术资讯 952 次浏览
这篇文章主要介绍了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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
1、块级作用域想想此时运行下面的程序会有输出吗?执行会成功吗? 块级作用域if1==1:name="lzl"print(name)foriinrange(10):age=iprint(age
本文讲述如何使用 tkinter 的布局管理 (被称作 layout managers 或 geometry managers). tkinter 有三种布局管理方式:pack,grid,place。注意这三种布局管理在同一个 master window 里一定不可以混用! 布局管理有以下功能:
这篇文章主要介绍了解决tensorflow 与keras 混用之坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
对List进行排序,Python提供了两个方法方法1 用List的内建函数list sort进行排序list sort(func=None, key=None, reverse=False)Python实
今天给大家带来的是关于Python的相关知识,文章围绕着python csv的一些基本操作展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008