python密码学各种加密模块教程
作者:菜鸟教程 时间:2021-03-10 05:32:55
在本章中,您将详细了解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 ,在实现哈希函数之前,将随机序列添加到密码字符串中.
来源:https://www.it1352.com/OnLineTutorial/cryptography_with_python/cryptography_with_python_modules_of_cryptography.html
标签:python,密码学,加密模块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
轻松实现php文件上传功能
2023-11-17 04:34:12
![](https://img.aspxhome.com/file/2023/2/100032_0s.gif)
asp两组字符串数据比较合并相同数据
2011-04-14 11:08:00
浅谈Python几种常见的归一化方法
2021-01-22 16:36:45
![](https://img.aspxhome.com/file/2023/4/66824_0s.gif)
python检查字符串是否是正确ISBN的方法
2022-05-10 14:54:01
如何向scrapy中的spider传递参数的几种方法
2021-12-16 20:04:48
JS 调试中常见的报错问题解决方法
2023-07-16 07:42:13
python字符串判断密码强弱
2021-05-09 04:20:04
如何解决“cint和clng的溢出出错”问题?
2009-12-03 20:21:00
ASP 函数语法速查表
2010-03-17 20:59:00
python实现图片二值化及灰度处理方式
2022-06-29 23:35:34
selenium python 实现基本自动化测试的示例代码
2021-05-04 06:23:07
![](https://img.aspxhome.com/file/2023/8/75898_0s.png)
Python实现将wav转amr,并转换成hex数组
2023-06-29 08:18:59
![](https://img.aspxhome.com/file/2023/0/59650_0s.png)
网站设计输入了些什么?
2008-04-01 09:30:00
js实时获得服务器上时间
2008-11-25 13:55:00
Django CBV与FBV原理及实例详解
2023-02-14 20:39:01
![](https://img.aspxhome.com/file/2023/8/93838_0s.png)
Python 从相对路径下import的方法
2023-06-15 03:16:10
python 计算t分布的双侧置信区间
2023-08-01 03:06:05
![](https://img.aspxhome.com/file/2023/9/61489_0s.jpg)
JavaScript的replace方法与正则表达式结合应用讲解
2008-03-06 21:37:00
Pytorch从0实现Transformer的实践
2021-12-22 03:26:41
![](https://img.aspxhome.com/file/2023/4/79424_0s.jpg)
在oracle 数据库中查看一个sql语句的执行时间和SP2-0027错误
2009-10-09 13:04:00