Python中各种加密模块的应用是怎样的
Admin 2022-08-18 群英技术资讯 447 次浏览
它包含所有配方和基元,并在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 for循环的内,一些朋友对于 for循环内输出和外输出方式不是很清楚,对此下面就和大家一起探讨一下。
Python 3 的标准库中没多少用来解决加密的,不过却有用于处理哈希的库。在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的
在flask更新到1.0之后的版本,官方推荐使用flask run的方式运行程序,可是作为开发,如果没有了pycharm的断点调试,这可太难受了。 所以,本篇博客主要讲述如何在pycharm中运行flask程序,并开启Debug模式。
python对XML的解析常见的 XML 编程接口有 DOM 和 SAX,这两种接口处理 XML 文件的方式不同,当然使用场合也不同。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008