Python中各种加密模块的应用是怎样的
Admin 2022-08-18 群英技术资讯 482 次浏览
它包含所有配方和基元,并在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中not not x 与 bool(x) 的区别,我们就来做一个选择,就是 not not x 和 bool(x) 用哪个比较好?下面一起进入文章看看吧</P><P>
这篇我们来了解Python自动化测试之异常处理机制,小编通过实际的案例向大家展示了操作过程,简单易懂,有需要的朋友可以参考了解看看,那么接下来就跟随小编的思路来往下学习吧,希望对大家学习或工作能有帮助。
这篇文章主要为大家介绍了python人工智能tensorflow函数tensorboard使用方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
既然在Pathlib库中提到了glob()函数,那么我们就专门用一篇内容讲解文件名的匹配。其实我们有专门的一个文件名匹配库就叫:glob。
今天就给大家分享一些python常用的编程模块吧,包括文件流的读写及如何删除str中的特定字符,感兴趣的朋友跟随一起学习下吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008